Skip to content

Commit

Permalink
fix(runner): handle fixture teardown error (#4683)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Dec 19, 2023
1 parent e975b57 commit c6f5f7f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
12 changes: 11 additions & 1 deletion packages/runner/src/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export function withFixtures(fn: Function, testContext?: TestContext) {
if (fixture.isFn) {
// wait for `use` call to extract fixture value
const useFnArgPromise = createDefer()
let isUseFnArgResolved = false
const fixtureReturn = fixture.value(context, async (useFnArg: unknown) => {
isUseFnArgResolved = true
useFnArgPromise.resolve(useFnArg)
// suspend fixture teardown until cleanup
const useReturnPromise = createDefer<void>()
Expand All @@ -100,7 +102,15 @@ export function withFixtures(fn: Function, testContext?: TestContext) {
await fixtureReturn
})
await useReturnPromise
}).catch(useFnArgPromise.reject) // treat fixture function error (until `use` call) as test failure
}).catch((e: unknown) => {
// treat fixture setup error as test failure
if (!isUseFnArgResolved) {
useFnArgPromise.reject(e)
return
}
// otherwise re-throw to avoid silencing error during cleanup
throw e
})
resolvedValue = await useFnArgPromise
}
else {
Expand Down
13 changes: 13 additions & 0 deletions test/fails/fixtures/test-extend/fixture-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,16 @@ describe('correctly fails when test times out', () => {
expect(a).toBe(2)
}, 20)
})

describe('error thrown during fixture teardown', () => {
const myTest = test.extend<{ a: string }>({
a: async ({}, use) => {
await use("hello");
throw new Error('Error fixture teardown')
},
})

myTest('no error in test', ({ a }) => {
expect(a).toBe("hello");
})
})
3 changes: 2 additions & 1 deletion test/fails/test/__snapshots__/runner.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ TypeError: failure"
exports[`should fail test-extend/circular-dependency.test.ts > test-extend/circular-dependency.test.ts 1`] = `"Error: Circular fixture dependency detected: a <- b <- a"`;
exports[`should fail test-extend/fixture-error.test.ts > test-extend/fixture-error.test.ts 1`] = `
"Error: Test timed out in 20ms.
"Error: Error fixture teardown
Error: Test timed out in 20ms.
Error: Error thrown in test fixture
Error: Error thrown in afterEach fixture
Error: Error thrown in beforeEach fixture"
Expand Down

0 comments on commit c6f5f7f

Please sign in to comment.