-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path-define.ts
122 lines (105 loc) · 3.84 KB
/
-define.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
import type { ModuleCallback, ModuleInfo, OrderedMap, TestCallback, TestContext, TestInfo } from './-types';
import { assert, generateHash } from './-utils';
import { Config, getCurrentModule, HooksDelegate, setCurrentModule } from './internals/config';
export { registerReporter } from './internals/delegating-reporter';
export { setupGlobalHooks, configure } from './internals/config';
export { PublicTestInfo } from './internals/run';
export const Modules: OrderedMap<ModuleInfo<TestContext>> = {
byName: new Map(),
byOrder: [],
};
export type { Diagnostic, Hooks as NestedHooks, GlobalHooks, TestContext } from './-types';
export function module<TC extends TestContext = TestContext>(name: string, cb: ModuleCallback<TC>): void {
const parentModule = getCurrentModule<TC>() ?? null;
let moduleName = name;
if (parentModule) {
moduleName = `${parentModule.name} > ${name}`;
} else {
Config.totals.primaryModules++;
}
Config.totals.modules++;
assert(`Cannot add the same module name twice: ${moduleName}`, !Modules.byName.has(moduleName));
const moduleConfig: ModuleInfo<TC>['config'] = {
beforeEach: [],
afterEach: [],
beforeModule: [],
afterModule: [],
};
const tests: OrderedMap<TestInfo<TC>> = { byName: new Map(), byOrder: [] };
const modules: OrderedMap<ModuleInfo<TC>> = { byName: new Map(), byOrder: [] };
const moduleInfo = {
moduleName,
name,
cb,
config: moduleConfig,
tests,
modules,
parent: parentModule,
};
setCurrentModule(moduleInfo);
if (parentModule) {
parentModule.modules.byName.set(name, moduleInfo);
parentModule.modules.byOrder.push(moduleInfo);
} else {
// @ts-expect-error TS poorly handles subtype constraints
Modules.byName.set(name, moduleInfo);
// @ts-expect-error TS poorly handles subtype constraints
Modules.byOrder.push(moduleInfo);
}
cb(HooksDelegate);
setCurrentModule(parentModule as unknown as ModuleInfo<TC>);
}
export function test<TC extends TestContext = TestContext>(name: string, cb: TestCallback<TC>): void {
const currentModule = getCurrentModule<TC>();
assert(`Cannot add a test outside of a module`, !!currentModule);
assert(`Cannot add the same test name twice: ${name}`, !currentModule.tests.byName.has(name));
Config.totals.tests++;
const testName = currentModule.moduleName + ' > ' + name;
const testInfo = {
id: generateHash(testName),
name,
testName,
cb,
skip: false,
todo: false,
module: currentModule,
};
currentModule.tests.byName.set(name, testInfo);
currentModule.tests.byOrder.push(testInfo);
}
export function todo<TC extends TestContext = TestContext>(name: string, cb: TestCallback<TC>): void {
const currentModule = getCurrentModule<TC>();
assert(`Cannot add a test outside of a module`, !!currentModule);
assert(`Cannot add the same test name twice: ${name}`, !currentModule.tests.byName.has(name));
Config.totals.todo++;
const testName = currentModule.moduleName + ' > ' + name;
const testInfo = {
id: generateHash(testName),
name,
testName,
cb,
skip: false,
todo: true,
module: currentModule,
};
currentModule.tests.byName.set(name, testInfo);
currentModule.tests.byOrder.push(testInfo);
}
export function skip<TC extends TestContext = TestContext>(name: string, cb: TestCallback<TC>): void {
const currentModule = getCurrentModule<TC>();
assert(`Cannot add a test outside of a module`, !!currentModule);
assert(`Cannot add the same test name twice: ${name}`, !currentModule.tests.byName.has(name));
Config.totals.skipped++;
const testName = currentModule.moduleName + ' > ' + name;
const testInfo = {
id: generateHash(testName),
name,
testName,
cb,
skip: true,
todo: false,
module: currentModule,
};
currentModule.tests.byName.set(name, testInfo);
currentModule.tests.byOrder.push(testInfo);
}