-
Notifications
You must be signed in to change notification settings - Fork 34
/
vitest.ts
85 lines (74 loc) · 2.41 KB
/
vitest.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
import { createBlockFn } from './blocks';
import { configureSnapshot } from './configure-snapshot';
import { isInNode } from './is-in-node';
import { state } from './state';
import { afterAllFn, afterEachFn } from './teardown';
import { browserMock } from './browser-mock';
import type * as VitestType from 'vitest';
import { makeExpect } from './expect';
import { ensureImported } from './ensure-imported';
type Vitest = typeof VitestType;
ensureImported('describe', 'describe');
ensureImported('it', 'test/it');
ensureImported('expect', 'expect');
ensureImported('beforeEach', 'beforeEach');
ensureImported('beforeAll', 'beforeAll');
ensureImported('afterEach', 'afterEach');
ensureImported('afterAll', 'afterAll');
const exportedExpect = makeExpect(expect);
const globalSetup = () => {
state.isGlobalSetupTeardownRegistered = true;
afterEach(afterEachFn);
afterAll(afterAllFn);
configureSnapshot(expect);
};
const makeDescribe: (actualThing: Function) => any =
(actualThing: Function) =>
(name: string, fn: () => void, ...extraArgs: any[]) =>
createBlockFn(
name,
fn,
extraArgs,
(...args: any[]) => {
if (isInNode && !state.isGlobalSetupTeardownRegistered) {
globalSetup();
}
return actualThing(...args);
},
true
);
const makeIt: (actualThing: Function) => any =
(actualThing: Function) =>
(name: string, fn: () => void, ...extraArgs: any[]) =>
createBlockFn(
name,
fn,
extraArgs,
(...args: any[]) => {
if (isInNode && !state.isGlobalSetupTeardownRegistered) {
globalSetup();
}
return actualThing(...args);
},
false
);
const exportedDescribe: Vitest['describe'] = makeDescribe(describe);
exportedDescribe.only = makeDescribe(describe.only);
exportedDescribe.skip = makeDescribe(describe.skip);
exportedDescribe.todo = makeDescribe((describe as any)?.todo);
const exportedIt: Vitest['it'] & { debug: Vitest['it'] } = makeIt(it);
exportedIt.only = makeIt(it.only);
exportedIt.skip = makeIt(it.skip);
exportedIt.todo = ((name: string) =>
createBlockFn(name, undefined as any, [], it.todo, false)) as any;
exportedIt.debug = ((...args: Parameters<Vitest['it']>) => {
const testKey = makeIt(it.only)(...args);
state.debugging.add(testKey);
}) as any;
export {
exportedDescribe as describe,
exportedIt as it,
exportedIt as test,
exportedExpect as expect,
browserMock,
};