Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,17 @@ export class Vitest {
await this.coverageProvider?.clean()
}

await this.initializeGlobalSetup(specs)
try {
await this.initializeGlobalSetup(specs)
} catch (err) {
// record the error before `finally` ends the run, otherwise
// reporters see no modules and print "No test files found"
this.state.catchError(err, 'Unhandled Error')
return {
testModules: [],
unhandledErrors: this.state.getUnhandledErrors(),
}
}

try {
const sorted = await this.getSequencer().sort(specs)
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/test/global-setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ it('should fail', async () => {
expect(stderr).toContain('globalSetup/error.ts:6:9')
})

it('reports the global setup error instead of "No test files found"', async () => {
const root = resolve(import.meta.dirname, '../fixtures/global-setup-fail')
const { stdout, exitCode, errorTree } = await runVitest({ root })

expect(exitCode).toBe(1)
expect(stdout).not.toContain('No test files found')
expect(errorTree()).toMatchInlineSnapshot(`
{
"__unhandled_errors__": [
"error",
],
}
`)
})

it('fails with a non-zero exit code when teardown throws', async () => {
const root = resolve(import.meta.dirname, '../fixtures/global-setup-teardown-fail')
const { stderr, exitCode } = await runVitest({ root })
Expand Down
Loading