diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index d234368fcf38..bf4a2663424d 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -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) diff --git a/test/e2e/test/global-setup.test.ts b/test/e2e/test/global-setup.test.ts index 31ee504fd448..3302ced07f9c 100644 --- a/test/e2e/test/global-setup.test.ts +++ b/test/e2e/test/global-setup.test.ts @@ -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 })