Skip to content

Commit 1904c9c

Browse files
authoredJan 23, 2023
fix: call afterAll, if beforeAll failed (#2737)
1 parent 998ea80 commit 1904c9c

File tree

4 files changed

+56
-21
lines changed

4 files changed

+56
-21
lines changed
 

‎packages/runner/src/run.ts

+23-16
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,15 @@ export async function runTest(test: Test, runner: VitestRunner) {
150150
test.result.state = 'pass'
151151
}
152152
catch (e) {
153-
const error = processError(e)
154-
test.result.state = 'fail'
155-
test.result.error = error
156-
test.result.errors = [error]
153+
failTask(test.result, e)
157154
}
158155

159156
try {
160157
await callSuiteHook(test.suite, test, 'afterEach', runner, [test.context, test.suite])
161158
await callCleanupHooks(beforeEachCleanups)
162159
}
163160
catch (e) {
164-
const error = processError(e)
165-
test.result.state = 'fail'
166-
test.result.error = error
167-
test.result.errors = [error]
161+
failTask(test.result, e)
168162
}
169163

170164
if (test.result.state === 'pass')
@@ -201,6 +195,14 @@ export async function runTest(test: Test, runner: VitestRunner) {
201195
updateTask(test, runner)
202196
}
203197

198+
function failTask(result: TaskResult, err: unknown) {
199+
result.state = 'fail'
200+
const error = processError(err)
201+
result.error = error
202+
result.errors ??= []
203+
result.errors.push(error)
204+
}
205+
204206
function markTasksAsSkipped(suite: Suite, runner: VitestRunner) {
205207
suite.tasks.forEach((t) => {
206208
t.mode = 'skip'
@@ -229,6 +231,8 @@ export async function runSuite(suite: Suite, runner: VitestRunner) {
229231

230232
updateTask(suite, runner)
231233

234+
let beforeAllCleanups: HookCleanupCallback[] = []
235+
232236
if (suite.mode === 'skip') {
233237
suite.result.state = 'skip'
234238
}
@@ -237,7 +241,7 @@ export async function runSuite(suite: Suite, runner: VitestRunner) {
237241
}
238242
else {
239243
try {
240-
const beforeAllCleanups = await callSuiteHook(suite, suite, 'beforeAll', runner, [suite])
244+
beforeAllCleanups = await callSuiteHook(suite, suite, 'beforeAll', runner, [suite])
241245

242246
if (runner.runSuite) {
243247
await runner.runSuite(suite)
@@ -262,17 +266,20 @@ export async function runSuite(suite: Suite, runner: VitestRunner) {
262266
}
263267
}
264268
}
265-
266-
await callSuiteHook(suite, suite, 'afterAll', runner, [suite])
267-
await callCleanupHooks(beforeAllCleanups)
268269
}
269270
catch (e) {
270-
const error = processError(e)
271-
suite.result.state = 'fail'
272-
suite.result.error = error
273-
suite.result.errors = [error]
271+
failTask(suite.result, e)
274272
}
275273
}
274+
275+
try {
276+
await callSuiteHook(suite, suite, 'afterAll', runner, [suite])
277+
await callCleanupHooks(beforeAllCleanups)
278+
}
279+
catch (e) {
280+
failTask(suite.result, e)
281+
}
282+
276283
suite.result.duration = now() - start
277284

278285
if (suite.mode === 'run') {
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { afterAll, beforeAll, expect, test } from 'vitest'
2+
3+
beforeAll(() => {
4+
// should both appear in snapshot
5+
throw new Error('before all')
6+
})
7+
8+
afterAll(() => {
9+
// should both appear in snapshot
10+
throw new Error('after all')
11+
})
12+
13+
test('1 = 1', () => {
14+
expect(1).toBe(1)
15+
})

‎test/fails/test/__snapshots__/runner.test.ts.snap

+15-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,25 @@ exports[`should fails > expect.test.ts > expect.test.ts 1`] = `"AssertionError:
1010
1111
exports[`should fails > hook-timeout.test.ts > hook-timeout.test.ts 1`] = `"Error: Hook timed out in 10ms."`;
1212
13+
exports[`should fails > hooks-called.test.ts > hooks-called.test.ts 1`] = `
14+
"Error: after all
15+
Error: before all"
16+
`;
17+
1318
exports[`should fails > mock-import-proxy-module.test.ts > mock-import-proxy-module.test.ts 1`] = `"Error: There are some problems in resolving the mocks API."`;
1419
1520
exports[`should fails > nested-suite.test.ts > nested-suite.test.ts 1`] = `"AssertionError: expected true to be false // Object.is equality"`;
1621
17-
exports[`should fails > stall.test.ts > stall.test.ts 1`] = `"TypeError: failure"`;
22+
exports[`should fails > stall.test.ts > stall.test.ts 1`] = `
23+
"TypeError: failure
24+
TypeError: failure
25+
TypeError: failure
26+
TypeError: failure"
27+
`;
1828
1929
exports[`should fails > test-timeout.test.ts > test-timeout.test.ts 1`] = `"Error: Test timed out in 10ms."`;
2030
21-
exports[`should fails > unhandled.test.ts > unhandled.test.ts 1`] = `"Error: some error"`;
31+
exports[`should fails > unhandled.test.ts > unhandled.test.ts 1`] = `
32+
"Error: some error
33+
Error: Uncaught [Error: some error]"
34+
`;

‎test/fails/test/runner.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ describe('should fails', async () => {
3030
const msg = String(error)
3131
.split(/\n/g)
3232
.reverse()
33-
.find(i => i.includes('Error: '))
34-
?.trim()
35-
.replace(root, '<rootDir>')
33+
.filter(i => i.includes('Error: ') && !i.includes('Command failed') && !i.includes('stackStr') && !i.includes('at runTest'))
34+
.map(i => i.trim().replace(root, '<rootDir>'),
35+
).join('\n')
3636
expect(msg).toMatchSnapshot(file)
3737
}, 30_000)
3838
}

0 commit comments

Comments
 (0)
Please sign in to comment.