Skip to content

Commit

Permalink
fix: call afterAll, if beforeAll failed (#2737)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 23, 2023
1 parent 998ea80 commit 1904c9c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
39 changes: 23 additions & 16 deletions packages/runner/src/run.ts
Expand Up @@ -150,21 +150,15 @@ export async function runTest(test: Test, runner: VitestRunner) {
test.result.state = 'pass'
}
catch (e) {
const error = processError(e)
test.result.state = 'fail'
test.result.error = error
test.result.errors = [error]
failTask(test.result, e)
}

try {
await callSuiteHook(test.suite, test, 'afterEach', runner, [test.context, test.suite])
await callCleanupHooks(beforeEachCleanups)
}
catch (e) {
const error = processError(e)
test.result.state = 'fail'
test.result.error = error
test.result.errors = [error]
failTask(test.result, e)
}

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

function failTask(result: TaskResult, err: unknown) {
result.state = 'fail'
const error = processError(err)
result.error = error
result.errors ??= []
result.errors.push(error)
}

function markTasksAsSkipped(suite: Suite, runner: VitestRunner) {
suite.tasks.forEach((t) => {
t.mode = 'skip'
Expand Down Expand Up @@ -229,6 +231,8 @@ export async function runSuite(suite: Suite, runner: VitestRunner) {

updateTask(suite, runner)

let beforeAllCleanups: HookCleanupCallback[] = []

if (suite.mode === 'skip') {
suite.result.state = 'skip'
}
Expand All @@ -237,7 +241,7 @@ export async function runSuite(suite: Suite, runner: VitestRunner) {
}
else {
try {
const beforeAllCleanups = await callSuiteHook(suite, suite, 'beforeAll', runner, [suite])
beforeAllCleanups = await callSuiteHook(suite, suite, 'beforeAll', runner, [suite])

if (runner.runSuite) {
await runner.runSuite(suite)
Expand All @@ -262,17 +266,20 @@ export async function runSuite(suite: Suite, runner: VitestRunner) {
}
}
}

await callSuiteHook(suite, suite, 'afterAll', runner, [suite])
await callCleanupHooks(beforeAllCleanups)
}
catch (e) {
const error = processError(e)
suite.result.state = 'fail'
suite.result.error = error
suite.result.errors = [error]
failTask(suite.result, e)
}
}

try {
await callSuiteHook(suite, suite, 'afterAll', runner, [suite])
await callCleanupHooks(beforeAllCleanups)
}
catch (e) {
failTask(suite.result, e)
}

suite.result.duration = now() - start

if (suite.mode === 'run') {
Expand Down
15 changes: 15 additions & 0 deletions test/fails/fixtures/hooks-called.test.ts
@@ -0,0 +1,15 @@
import { afterAll, beforeAll, expect, test } from 'vitest'

beforeAll(() => {
// should both appear in snapshot
throw new Error('before all')
})

afterAll(() => {
// should both appear in snapshot
throw new Error('after all')
})

test('1 = 1', () => {
expect(1).toBe(1)
})
17 changes: 15 additions & 2 deletions test/fails/test/__snapshots__/runner.test.ts.snap
Expand Up @@ -10,12 +10,25 @@ exports[`should fails > expect.test.ts > expect.test.ts 1`] = `"AssertionError:
exports[`should fails > hook-timeout.test.ts > hook-timeout.test.ts 1`] = `"Error: Hook timed out in 10ms."`;
exports[`should fails > hooks-called.test.ts > hooks-called.test.ts 1`] = `
"Error: after all
Error: before all"
`;
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."`;
exports[`should fails > nested-suite.test.ts > nested-suite.test.ts 1`] = `"AssertionError: expected true to be false // Object.is equality"`;
exports[`should fails > stall.test.ts > stall.test.ts 1`] = `"TypeError: failure"`;
exports[`should fails > stall.test.ts > stall.test.ts 1`] = `
"TypeError: failure
TypeError: failure
TypeError: failure
TypeError: failure"
`;
exports[`should fails > test-timeout.test.ts > test-timeout.test.ts 1`] = `"Error: Test timed out in 10ms."`;
exports[`should fails > unhandled.test.ts > unhandled.test.ts 1`] = `"Error: some error"`;
exports[`should fails > unhandled.test.ts > unhandled.test.ts 1`] = `
"Error: some error
Error: Uncaught [Error: some error]"
`;
6 changes: 3 additions & 3 deletions test/fails/test/runner.test.ts
Expand Up @@ -30,9 +30,9 @@ describe('should fails', async () => {
const msg = String(error)
.split(/\n/g)
.reverse()
.find(i => i.includes('Error: '))
?.trim()
.replace(root, '<rootDir>')
.filter(i => i.includes('Error: ') && !i.includes('Command failed') && !i.includes('stackStr') && !i.includes('at runTest'))
.map(i => i.trim().replace(root, '<rootDir>'),
).join('\n')
expect(msg).toMatchSnapshot(file)
}, 30_000)
}
Expand Down

0 comments on commit 1904c9c

Please sign in to comment.