-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathconfig.ts
236 lines (221 loc) · 5.98 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* global Testem */
import type {
GlobalCallback,
GlobalConfig,
GlobalHooks,
HooksCallback,
ModuleInfo,
ParamConfig,
TestContext,
} from '../-types';
import { assert } from '../-utils';
const urlParams = new URLSearchParams(window.location.search);
const search = urlParams.get('search');
const tests = new Set(urlParams.getAll('t'));
const modules = new Set(urlParams.getAll('m'));
export const Config: GlobalConfig = {
globalHooks: {
beforeEach: [],
afterEach: [],
beforeModule: [],
afterModule: [],
onSuiteStart: [],
onSuiteFinish: [],
},
// @ts-expect-error
useTestem: typeof Testem !== 'undefined',
// @ts-expect-error
useDiagnostic: typeof Testem === 'undefined',
testTimeoutMs: 50,
concurrency: 1,
modules,
tests,
params: {
search: {
id: 'search',
label: 'Filter Tests',
value: search ?? '',
},
hideReport: {
id: 'hideReport',
label: 'Hide Report',
value: true,
},
concurrency: {
id: 'concurrency',
label: 'Enable Concurrency',
value: false,
},
memory: {
id: 'memory',
label: 'Instrument Memory',
value: false,
},
instrument: {
id: 'performance',
label: 'Instrument Performance',
value: true,
},
groupLogs: {
id: 'groupLogs',
label: 'Group Logs',
value: true,
},
debug: {
id: 'debug',
label: 'Debug Mode',
value: false,
},
container: {
id: 'container',
label: 'Hide Container',
value: true,
},
tryCatch: {
id: 'tryCatch',
label: 'No Try/Catch',
value: true,
},
},
totals: {
tests: 0,
primaryModules: 0,
modules: 0,
skipped: 0,
todo: 0,
},
_current: null,
};
let currentModule: ModuleInfo<TestContext>;
let isResolvingGlobalHooks = false;
export const HooksDelegate = {
beforeEach<TC extends TestContext>(cb: HooksCallback<TC>): void {
if (isResolvingGlobalHooks) {
// @ts-expect-error TS poorly handles subtype constraints
Config.globalHooks.beforeEach.push(cb);
} else {
// @ts-expect-error TS poorly handles subtype constraints
currentModule.config.beforeEach.push(cb);
}
},
afterEach<TC extends TestContext>(cb: HooksCallback<TC>): void {
if (isResolvingGlobalHooks) {
// @ts-expect-error TS poorly handles subtype constraints
Config.globalHooks.afterEach.push(cb);
} else {
// @ts-expect-error TS poorly handles subtype constraints
currentModule.config.afterEach.push(cb);
}
},
beforeModule(cb: GlobalCallback): void {
if (isResolvingGlobalHooks) {
Config.globalHooks.beforeModule.push(cb);
} else {
currentModule.config.beforeModule.push(cb);
}
},
afterModule(cb: GlobalCallback): void {
if (isResolvingGlobalHooks) {
Config.globalHooks.afterModule.push(cb);
} else {
currentModule.config.afterModule.push(cb);
}
},
onSuiteStart(cb: GlobalCallback): void {
assert(`Cannot add a global onSuiteStart hook inside of a module`, isResolvingGlobalHooks);
Config.globalHooks.onSuiteStart.push(cb);
},
onSuiteFinish(cb: GlobalCallback): void {
assert(`Cannot add a global onSuiteFinish hook inside of a module`, isResolvingGlobalHooks);
Config.globalHooks.onSuiteFinish.push(cb);
},
};
export function getCurrentModule<TC extends TestContext>(): ModuleInfo<TC> {
return currentModule;
}
export function setCurrentModule<TC extends TestContext>(module: ModuleInfo<TC>) {
// @ts-expect-error TS poorly handles subtype constraints
currentModule = module;
}
export function setupGlobalHooks<TC extends TestContext>(cb: (hooks: GlobalHooks<TC>) => void): void {
isResolvingGlobalHooks = true;
cb(HooksDelegate);
isResolvingGlobalHooks = false;
}
export type ConfigOptions = {
concurrency: number;
instrument: boolean;
tryCatch: boolean;
debug: boolean;
groupLogs: boolean;
memory: boolean;
container: boolean;
hideReport: boolean;
params: Record<string, ParamConfig>;
useTestem: boolean;
useDiagnostic: boolean;
testTimeoutMs: number;
};
const configOptions = [
'concurrency',
'tryCatch',
'instrument',
'hideReport',
'memory',
'groupLogs',
'debug',
'container',
] as const;
export function configure(options: Partial<ConfigOptions>): void {
if (options.useTestem && options.useDiagnostic) {
throw new Error(
`Cannot use both Testem and Diagnostic at the same time. Please remove one of these options or set it to false.`
);
}
if ('useTestem' in options && typeof options.useTestem === 'boolean') {
Config.useTestem = options.useTestem;
Config.useDiagnostic = !options.useTestem;
}
if ('useDiagnostic' in options && typeof options.useDiagnostic === 'boolean') {
Config.useDiagnostic = options.useDiagnostic;
Config.useTestem = !options.useDiagnostic;
}
if ('concurrency' in options && typeof options.concurrency === 'number') {
Config.concurrency = options.concurrency;
// @ts-expect-error
options.concurrency = options.concurrency > 1;
}
configOptions.forEach((key) => {
if (key in options && typeof options[key] === 'boolean') {
Config.params[key].value = options[key];
}
// don't allow setting these params via configure
if (options.params?.[key]) {
delete options.params[key];
}
});
Config.testTimeoutMs = options.testTimeoutMs ?? 0;
// copy over any remaining params
Object.assign(Config.params, options.params);
}
export function getSettings() {
return {
useTestem: Config.useTestem,
useDiagnostic: Config.useDiagnostic,
concurrency: Config.concurrency,
params: Config.params,
};
}
export function instrument() {
return (Config.params.instrument.value || null) as unknown as PerformanceMark;
}
export function groupLogs() {
return Config.params.groupLogs.value;
}
// 0 - stop
// 1 - start
// 2 - back
// 3 - forward
// 4 - restart
export function updateSuiteState(value: number) {}
export function updateConfigValue(key: string, value: boolean) {}