diff --git a/packages/vitest/src/node/test-run.ts b/packages/vitest/src/node/test-run.ts index e9a3345407d7..f1e557ba0555 100644 --- a/packages/vitest/src/node/test-run.ts +++ b/packages/vitest/src/node/test-run.ts @@ -182,6 +182,13 @@ export class TestRun { assert(task && entity, `Entity must be found for task ${task?.name || id}`) + if (event === 'suite-failed-early' && entity.type === 'module') { + // the file failed during import + await this.vitest.report('onTestModuleStart', entity) + await this.vitest.report('onTestModuleEnd', entity) + return + } + if (event === 'suite-prepare' && entity.type === 'suite') { return await this.vitest.report('onTestSuiteReady', entity) } diff --git a/test/cli/test/fails.test.ts b/test/cli/test/fails.test.ts index 664c0a1c4072..f2f7c9a3e0a9 100644 --- a/test/cli/test/fails.test.ts +++ b/test/cli/test/fails.test.ts @@ -129,3 +129,37 @@ it('prints a warning if the assertion is not awaited in the browser mode', async expect(stderr).toContain('Promise returned by \`expect(actual).resolves.toBe(expected)\` was not awaited') expect(stderr).toContain('base.test.js:5:33') }) + +it('reports test file if it failed to load', async () => { + const hooks: string[] = [] + await runInlineTests({ + 'basic.test.js': `throw new Error('fail')`, + }, { + reporters: [ + 'default', + { + onTestModuleQueued(testModule) { + hooks.push(`onTestModuleQueued:${testModule.relativeModuleId}`) + }, + onTestModuleStart(testModule) { + hooks.push(`onTestModuleStart:${testModule.relativeModuleId}`) + }, + onTestModuleCollected(testModule) { + hooks.push(`onTestModuleCollected:${testModule.relativeModuleId}`) + }, + onTestModuleEnd(testModule) { + hooks.push(`onTestModuleEnd:${testModule.relativeModuleId}`) + }, + }, + ], + }) + + expect(hooks).toMatchInlineSnapshot(` + [ + "onTestModuleQueued:basic.test.js", + "onTestModuleCollected:basic.test.js", + "onTestModuleStart:basic.test.js", + "onTestModuleEnd:basic.test.js", + ] + `) +})