Skip to content

Commit

Permalink
fix: handle errors thrown in fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyddall authored and sheremet-va committed Nov 10, 2023
1 parent 9734e81 commit f6844ad
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/runner/src/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function withFixtures(fn: Function, testContext?: TestContext) {
return isFn ? value(context, use) : use(value)
}

const setupFixturePromise = next()
const setupFixturePromise = next().catch(reject)
cleanupFnArray.unshift(() => setupFixturePromise)
})
}
Expand Down
40 changes: 40 additions & 0 deletions test/fails/fixtures/test-extend/fixture-error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { afterEach, beforeEach, describe, expectTypeOf, test } from 'vitest'

describe('error thrown in beforeEach fixtures', () => {
const myTest = test.extend<{ a: never }>({
a: async () => {
throw new Error('Error thrown in beforeEach fixture')
},
})

// eslint-disable-next-line unused-imports/no-unused-vars
beforeEach<{ a: never }>(({ a }) => {})

myTest('error is handled', () => {})
})

describe('error thrown in afterEach fixtures', () => {
const myTest = test.extend<{ a: never }>({
a: async () => {
throw new Error('Error thrown in afterEach fixture')
},
})

// eslint-disable-next-line unused-imports/no-unused-vars
afterEach<{ a: never }>(({ a }) => {})

myTest('fixture errors', () => {
expectTypeOf(1).toEqualTypeOf<number>()
})
})

describe('error thrown in test fixtures', () => {
const myTest = test.extend<{ a: never }>({
a: async () => {
throw new Error('Error thrown in test fixture')
},
})

// eslint-disable-next-line unused-imports/no-unused-vars
myTest('fixture errors', ({ a }) => {})
})
6 changes: 6 additions & 0 deletions test/fails/test/__snapshots__/runner.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ TypeError: failure"
exports[`should fail test-extend/circular-dependency.test.ts > test-extend/circular-dependency.test.ts 1`] = `"Error: circular fixture dependency"`;
exports[`should fail test-extend/fixture-error.test.ts > test-extend/fixture-error.test.ts 1`] = `
"Error: Error thrown in test fixture
Error: Error thrown in afterEach fixture
Error: Error thrown in beforeEach fixture"
`;
exports[`should fail test-extend/fixture-rest-params.test.ts > test-extend/fixture-rest-params.test.ts 1`] = `"Error: the first argument must use object destructuring pattern"`;
exports[`should fail test-extend/fixture-rest-props.test.ts > test-extend/fixture-rest-props.test.ts 1`] = `"Error: Rest parameters are not supported"`;
Expand Down

0 comments on commit f6844ad

Please sign in to comment.