|
| 1 | +import type { ModuleCallback, ModuleInfo, OrderedMap, TestCallback, TestContext, TestInfo } from './-types'; |
| 2 | +import { assert, generateHash } from './-utils'; |
| 3 | +import { Config, getCurrentModule, HooksDelegate, setCurrentModule } from './internals/config'; |
| 4 | + |
| 5 | +export { registerReporter } from './internals/delegating-reporter'; |
| 6 | +export { setupGlobalHooks, configure } from './internals/config'; |
| 7 | +export { PublicTestInfo } from './internals/run'; |
| 8 | + |
| 9 | +export const Modules: OrderedMap<ModuleInfo<TestContext>> = { |
| 10 | + byName: new Map(), |
| 11 | + byOrder: [], |
| 12 | +}; |
| 13 | + |
| 14 | +export type { Diagnostic, Hooks as NestedHooks, GlobalHooks, TestContext } from './-types'; |
| 15 | + |
| 16 | +export function module<TC extends TestContext = TestContext>(name: string, cb: ModuleCallback<TC>): void { |
| 17 | + const parentModule = getCurrentModule<TC>() ?? null; |
| 18 | + let moduleName = name; |
| 19 | + if (parentModule) { |
| 20 | + moduleName = `${parentModule.name} > ${name}`; |
| 21 | + } else { |
| 22 | + Config.totals.primaryModules++; |
| 23 | + } |
| 24 | + Config.totals.modules++; |
| 25 | + |
| 26 | + assert(`Cannot add the same module name twice: ${moduleName}`, !Modules.byName.has(moduleName)); |
| 27 | + const moduleConfig: ModuleInfo<TC>['config'] = { |
| 28 | + beforeEach: [], |
| 29 | + afterEach: [], |
| 30 | + beforeModule: [], |
| 31 | + afterModule: [], |
| 32 | + }; |
| 33 | + const tests: OrderedMap<TestInfo<TC>> = { byName: new Map(), byOrder: [] }; |
| 34 | + const modules: OrderedMap<ModuleInfo<TC>> = { byName: new Map(), byOrder: [] }; |
| 35 | + const moduleInfo = { |
| 36 | + moduleName, |
| 37 | + name, |
| 38 | + cb, |
| 39 | + config: moduleConfig, |
| 40 | + tests, |
| 41 | + modules, |
| 42 | + parent: parentModule, |
| 43 | + }; |
| 44 | + |
| 45 | + setCurrentModule(moduleInfo); |
| 46 | + |
| 47 | + if (parentModule) { |
| 48 | + parentModule.modules.byName.set(name, moduleInfo); |
| 49 | + parentModule.modules.byOrder.push(moduleInfo); |
| 50 | + } else { |
| 51 | + // @ts-expect-error TS poorly handles subtype constraints |
| 52 | + Modules.byName.set(name, moduleInfo); |
| 53 | + // @ts-expect-error TS poorly handles subtype constraints |
| 54 | + Modules.byOrder.push(moduleInfo); |
| 55 | + } |
| 56 | + |
| 57 | + cb(HooksDelegate); |
| 58 | + setCurrentModule(parentModule as unknown as ModuleInfo<TC>); |
| 59 | +} |
| 60 | + |
| 61 | +export function test<TC extends TestContext = TestContext>(name: string, cb: TestCallback<TC>): void { |
| 62 | + const currentModule = getCurrentModule<TC>(); |
| 63 | + assert(`Cannot add a test outside of a module`, !!currentModule); |
| 64 | + assert(`Cannot add the same test name twice: ${name}`, !currentModule.tests.byName.has(name)); |
| 65 | + Config.totals.tests++; |
| 66 | + |
| 67 | + const testInfo = { |
| 68 | + id: generateHash(currentModule.moduleName + ' > ' + name), |
| 69 | + name, |
| 70 | + cb, |
| 71 | + skip: false, |
| 72 | + todo: false, |
| 73 | + module: currentModule, |
| 74 | + }; |
| 75 | + |
| 76 | + currentModule.tests.byName.set(name, testInfo); |
| 77 | + currentModule.tests.byOrder.push(testInfo); |
| 78 | +} |
| 79 | + |
| 80 | +export function todo<TC extends TestContext = TestContext>(name: string, cb: TestCallback<TC>): void { |
| 81 | + const currentModule = getCurrentModule<TC>(); |
| 82 | + assert(`Cannot add a test outside of a module`, !!currentModule); |
| 83 | + assert(`Cannot add the same test name twice: ${name}`, !currentModule.tests.byName.has(name)); |
| 84 | + Config.totals.todo++; |
| 85 | + |
| 86 | + const testInfo = { |
| 87 | + id: generateHash(currentModule.moduleName + ' > ' + name), |
| 88 | + name, |
| 89 | + cb, |
| 90 | + skip: false, |
| 91 | + todo: true, |
| 92 | + module: currentModule, |
| 93 | + }; |
| 94 | + |
| 95 | + currentModule.tests.byName.set(name, testInfo); |
| 96 | + currentModule.tests.byOrder.push(testInfo); |
| 97 | +} |
| 98 | + |
| 99 | +export function skip<TC extends TestContext = TestContext>(name: string, cb: TestCallback<TC>): void { |
| 100 | + const currentModule = getCurrentModule<TC>(); |
| 101 | + assert(`Cannot add a test outside of a module`, !!currentModule); |
| 102 | + assert(`Cannot add the same test name twice: ${name}`, !currentModule.tests.byName.has(name)); |
| 103 | + Config.totals.skipped++; |
| 104 | + |
| 105 | + const testInfo = { |
| 106 | + id: generateHash(currentModule.moduleName + ' > ' + name), |
| 107 | + name, |
| 108 | + cb, |
| 109 | + skip: true, |
| 110 | + todo: false, |
| 111 | + module: currentModule, |
| 112 | + }; |
| 113 | + |
| 114 | + currentModule.tests.byName.set(name, testInfo); |
| 115 | + currentModule.tests.byOrder.push(testInfo); |
| 116 | +} |
0 commit comments