/
run.ts
225 lines (183 loc) · 5.56 KB
/
run.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { performance } from 'perf_hooks'
import type { HookListener, ResolvedConfig, Suite, SuiteHooks, Task, TaskResult, Test } from '../types'
import { vi } from '../integrations/vi'
import { getSnapshotClient } from '../integrations/snapshot/chai'
import { getFullName, hasFailed, hasTests, partitionSuiteChildren } from '../utils'
import { getState, setState } from '../integrations/chai/jest-expect'
import { getFn, getHooks } from './map'
import { rpc } from './rpc'
import { collectTests } from './collect'
import { processError } from './error'
export async function callSuiteHook<T extends keyof SuiteHooks>(suite: Suite, name: T, args: SuiteHooks[T][0] extends HookListener<infer A> ? A : never) {
if (name === 'beforeEach' && suite.suite)
await callSuiteHook(suite.suite, name, args)
await Promise.all(getHooks(suite)[name].map(fn => fn(...(args as any))))
if (name === 'afterEach' && suite.suite)
await callSuiteHook(suite.suite, name, args)
}
const packs = new Map<string, TaskResult|undefined>()
let updateTimer: any
let previousUpdate: Promise<void>|undefined
function updateTask(task: Task) {
packs.set(task.id, task.result)
clearTimeout(updateTimer)
updateTimer = setTimeout(() => {
previousUpdate = sendTasksUpdate()
}, 10)
}
async function sendTasksUpdate() {
clearTimeout(updateTimer)
await previousUpdate
if (packs.size) {
const p = rpc().onTaskUpdate(Array.from(packs))
packs.clear()
return p
}
}
export async function runTest(test: Test) {
if (test.mode !== 'run')
return
if (test.result?.state === 'fail') {
updateTask(test)
return
}
const start = performance.now()
test.result = {
state: 'run',
}
updateTask(test)
clearModuleMocks()
getSnapshotClient().setTest(test)
__vitest_worker__.current = test
try {
await callSuiteHook(test.suite, 'beforeEach', [test, test.suite])
setState({
assertionCalls: 0,
isExpectingAssertions: false,
isExpectingAssertionsError: null,
expectedAssertionsNumber: null,
expectedAssertionsNumberError: null,
testPath: test.suite.file?.filepath,
currentTestName: getFullName(test),
})
await getFn(test)()
const { assertionCalls, expectedAssertionsNumber, expectedAssertionsNumberError, isExpectingAssertions, isExpectingAssertionsError } = getState()
if (expectedAssertionsNumber !== null && assertionCalls !== expectedAssertionsNumber)
throw expectedAssertionsNumberError
if (isExpectingAssertions === true && assertionCalls === 0)
throw isExpectingAssertionsError
test.result.state = 'pass'
}
catch (e) {
test.result.state = 'fail'
test.result.error = processError(e)
}
try {
await callSuiteHook(test.suite, 'afterEach', [test, test.suite])
}
catch (e) {
test.result.state = 'fail'
test.result.error = processError(e)
}
// if test is marked to be failed, flip the result
if (test.fails) {
if (test.result.state === 'pass') {
test.result.state = 'fail'
test.result.error = processError(new Error('Expect test to fail'))
}
else {
test.result.state = 'pass'
test.result.error = undefined
}
}
getSnapshotClient().clearTest()
test.result.duration = performance.now() - start
__vitest_worker__.current = undefined
updateTask(test)
}
function markTasksAsSkipped(suite: Suite) {
suite.tasks.forEach((t) => {
t.mode = 'skip'
t.result = { ...t.result, state: 'skip' }
updateTask(t)
if (t.type === 'suite') markTasksAsSkipped(t)
})
}
export async function runSuite(suite: Suite) {
if (suite.result?.state === 'fail') {
markTasksAsSkipped(suite)
updateTask(suite)
return
}
const start = performance.now()
suite.result = {
state: 'run',
}
updateTask(suite)
if (suite.mode === 'skip') {
suite.result.state = 'skip'
}
else if (suite.mode === 'todo') {
suite.result.state = 'todo'
}
else {
try {
await callSuiteHook(suite, 'beforeAll', [suite])
for (const tasksGroup of partitionSuiteChildren(suite)) {
if (tasksGroup[0].concurrent === true) {
await Promise.all(tasksGroup.map(c => runSuiteChild(c)))
}
else {
for (const c of tasksGroup)
await runSuiteChild(c)
}
}
await callSuiteHook(suite, 'afterAll', [suite])
}
catch (e) {
suite.result.state = 'fail'
suite.result.error = processError(e)
}
}
suite.result.duration = performance.now() - start
if (suite.mode === 'run') {
if (!hasTests(suite)) {
suite.result.state = 'fail'
if (!suite.result.error)
suite.result.error = new Error(`No tests found in suite ${suite.name}`)
}
else if (hasFailed(suite)) {
suite.result.state = 'fail'
}
else {
suite.result.state = 'pass'
}
}
updateTask(suite)
}
async function runSuiteChild(c: Task) {
return c.type === 'test'
? runTest(c)
: runSuite(c)
}
export async function runSuites(suites: Suite[]) {
for (const suite of suites)
await runSuite(suite)
}
export async function startTests(paths: string[], config: ResolvedConfig) {
const files = await collectTests(paths, config)
rpc().onCollected(files)
await runSuites(files)
await getSnapshotClient().saveSnap()
await sendTasksUpdate()
}
export function clearModuleMocks() {
const { clearMocks, mockReset, restoreMocks } = __vitest_worker__.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()
}