/
test.ts
157 lines (131 loc) · 4.85 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
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
import type { CancelReason, Custom, ExtendedContext, Suite, Task, TaskContext, Test, VitestRunner, VitestRunnerImportSource } from '@vitest/runner'
import type { ExpectStatic } from '@vitest/expect'
import { GLOBAL_EXPECT, getState, setState } from '@vitest/expect'
import { getSnapshotClient } from '../../integrations/snapshot/chai'
import { vi } from '../../integrations/vi'
import { getFullName, getNames, getTests, getWorkerState } from '../../utils'
import { createExpect } from '../../integrations/chai/index'
import type { ResolvedConfig } from '../../types/config'
import type { VitestExecutor } from '../execute'
import { rpc } from '../rpc'
export class VitestTestRunner implements VitestRunner {
private snapshotClient = getSnapshotClient()
protected workerState = getWorkerState()
private __vitest_executor!: VitestExecutor
private cancelRun = false
constructor(public config: ResolvedConfig) {}
importFile(filepath: string, source: VitestRunnerImportSource): unknown {
if (source === 'setup')
this.workerState.moduleCache.delete(filepath)
return this.__vitest_executor.executeId(filepath)
}
onBeforeRunFiles() {
this.snapshotClient.clear()
}
onAfterRunFiles() {
this.workerState.current = undefined
}
async onAfterRunSuite(suite: Suite) {
if (this.config.logHeapUsage && typeof process !== 'undefined')
suite.result!.heap = process.memoryUsage().heapUsed
if (suite.mode !== 'skip' && typeof suite.filepath !== 'undefined') {
// mark snapshots in skipped tests as not obsolete
for (const test of getTests(suite)) {
if (test.mode === 'skip') {
const name = getNames(test).slice(1).join(' > ')
this.snapshotClient.skipTestSnapshots(name)
}
}
const result = await this.snapshotClient.finishCurrentRun()
if (result)
await rpc().snapshotSaved(result)
}
this.workerState.current = suite.suite
}
onAfterRunTask(test: Task) {
this.snapshotClient.clearTest()
if (this.config.logHeapUsage && typeof process !== 'undefined')
test.result!.heap = process.memoryUsage().heapUsed
this.workerState.current = test.suite
}
onCancel(_reason: CancelReason) {
this.cancelRun = true
}
async onBeforeRunTask(test: Task) {
if (this.cancelRun)
test.mode = 'skip'
if (test.mode !== 'run')
return
clearModuleMocks(this.config)
this.workerState.current = test
}
async onBeforeRunSuite(suite: Suite) {
if (this.cancelRun)
suite.mode = 'skip'
// initialize snapshot state before running file suite
if (suite.mode !== 'skip' && typeof suite.filepath !== 'undefined') {
// default "name" is irrelevant for Vitest since each snapshot assertion
// (e.g. `toMatchSnapshot`) specifies "filepath" / "name" pair explicitly
await this.snapshotClient.startCurrentRun(suite.filepath, '__default_name_', this.workerState.config.snapshotOptions)
}
this.workerState.current = suite
}
onBeforeTryTask(test: Task) {
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])
}
onAfterTryTask(test: Task) {
const {
assertionCalls,
expectedAssertionsNumber,
expectedAssertionsNumberErrorGen,
isExpectingAssertions,
isExpectingAssertionsError,
// @ts-expect-error _local is untyped
} = 'context' in test && 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
}
extendTaskContext<T extends Test | Custom>(context: TaskContext<T>): ExtendedContext<T> {
let _expect: ExpectStatic | undefined
Object.defineProperty(context, 'expect', {
get() {
if (!_expect)
_expect = createExpect(context.task)
return _expect
},
})
Object.defineProperty(context, '_local', {
get() {
return _expect != null
},
})
return context as ExtendedContext<T>
}
}
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()
}