/
test.ts
120 lines (102 loc) · 3.53 KB
/
test.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
import type { Suite, Test, TestContext, VitestRunner, VitestRunnerImportSource } from '@vitest/runner'
import { GLOBAL_EXPECT, getState, setState } from '@vitest/expect'
import { getSnapshotClient } from '../../integrations/snapshot/chai'
import { vi } from '../../integrations/vi'
import { getFullName, getWorkerState } from '../../utils'
import { createExpect } from '../../integrations/chai/index'
import type { ResolvedConfig } from '../../types/config'
import type { VitestExecutor } from '../execute'
export class VitestTestRunner implements VitestRunner {
private snapshotClient = getSnapshotClient()
private workerState = getWorkerState()
private __vitest_executor!: VitestExecutor
constructor(public config: ResolvedConfig) {}
importFile(filepath: string, source: VitestRunnerImportSource): unknown {
if (source === 'setup')
this.workerState.moduleCache.delete(filepath)
return this.__vitest_executor.executeId(filepath)
}
onBeforeRun() {
this.snapshotClient.clear()
}
async onAfterRun() {
await this.snapshotClient.saveCurrent()
}
onAfterRunSuite(suite: Suite) {
if (this.config.logHeapUsage && typeof process !== 'undefined')
suite.result!.heap = process.memoryUsage().heapUsed
}
onAfterRunTest(test: Test) {
this.snapshotClient.clearTest()
if (this.config.logHeapUsage && typeof process !== 'undefined')
test.result!.heap = process.memoryUsage().heapUsed
this.workerState.current = undefined
}
async onBeforeRunTest(test: Test) {
if (test.mode !== 'run') {
this.snapshotClient.skipTestSnapshots(test)
return
}
clearModuleMocks(this.config)
await this.snapshotClient.setTest(test)
this.workerState.current = test
}
onBeforeTryTest(test: Test) {
setState({
assertionCalls: 0,
isExpectingAssertions: false,
isExpectingAssertionsError: null,
expectedAssertionsNumber: null,
expectedAssertionsNumberErrorGen: null,
testPath: test.suite.file?.filepath,
currentTestName: getFullName(test),
snapshotState: this.snapshotClient.snapshotState,
}, (globalThis as any)[GLOBAL_EXPECT])
}
onAfterTryTest(test: Test) {
const {
assertionCalls,
expectedAssertionsNumber,
expectedAssertionsNumberErrorGen,
isExpectingAssertions,
isExpectingAssertionsError,
// @ts-expect-error local is untyped
} = test.context._local
? test.context.expect.getState()
: getState((globalThis as any)[GLOBAL_EXPECT])
if (expectedAssertionsNumber !== null && assertionCalls !== expectedAssertionsNumber)
throw expectedAssertionsNumberErrorGen!()
if (isExpectingAssertions === true && assertionCalls === 0)
throw isExpectingAssertionsError
}
extendTestContext(context: TestContext): TestContext {
let _expect: Vi.ExpectStatic | undefined
Object.defineProperty(context, 'expect', {
get() {
if (!_expect)
_expect = createExpect(context.meta)
return _expect
},
})
Object.defineProperty(context, '_local', {
get() {
return _expect != null
},
})
return context
}
}
function clearModuleMocks(config: ResolvedConfig) {
const { clearMocks, mockReset, restoreMocks, unstubEnvs, unstubGlobals } = config
// since each function calls another, we can just call one
if (restoreMocks)
vi.restoreAllMocks()
else if (mockReset)
vi.resetAllMocks()
else if (clearMocks)
vi.clearAllMocks()
if (unstubEnvs)
vi.unstubAllEnvs()
if (unstubGlobals)
vi.unstubAllGlobals()
}