Skip to content

Commit c788829

Browse files
authored
fix(reporter): report test module if it failed to run (#9272)
1 parent b46d744 commit c788829

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

packages/vitest/src/node/test-run.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ export class TestRun {
182182

183183
assert(task && entity, `Entity must be found for task ${task?.name || id}`)
184184

185+
if (event === 'suite-failed-early' && entity.type === 'module') {
186+
// the file failed during import
187+
await this.vitest.report('onTestModuleStart', entity)
188+
await this.vitest.report('onTestModuleEnd', entity)
189+
return
190+
}
191+
185192
if (event === 'suite-prepare' && entity.type === 'suite') {
186193
return await this.vitest.report('onTestSuiteReady', entity)
187194
}

test/cli/test/fails.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,37 @@ it('prints a warning if the assertion is not awaited in the browser mode', async
129129
expect(stderr).toContain('Promise returned by \`expect(actual).resolves.toBe(expected)\` was not awaited')
130130
expect(stderr).toContain('base.test.js:5:33')
131131
})
132+
133+
it('reports test file if it failed to load', async () => {
134+
const hooks: string[] = []
135+
await runInlineTests({
136+
'basic.test.js': `throw new Error('fail')`,
137+
}, {
138+
reporters: [
139+
'default',
140+
{
141+
onTestModuleQueued(testModule) {
142+
hooks.push(`onTestModuleQueued:${testModule.relativeModuleId}`)
143+
},
144+
onTestModuleStart(testModule) {
145+
hooks.push(`onTestModuleStart:${testModule.relativeModuleId}`)
146+
},
147+
onTestModuleCollected(testModule) {
148+
hooks.push(`onTestModuleCollected:${testModule.relativeModuleId}`)
149+
},
150+
onTestModuleEnd(testModule) {
151+
hooks.push(`onTestModuleEnd:${testModule.relativeModuleId}`)
152+
},
153+
},
154+
],
155+
})
156+
157+
expect(hooks).toMatchInlineSnapshot(`
158+
[
159+
"onTestModuleQueued:basic.test.js",
160+
"onTestModuleCollected:basic.test.js",
161+
"onTestModuleStart:basic.test.js",
162+
"onTestModuleEnd:basic.test.js",
163+
]
164+
`)
165+
})

0 commit comments

Comments
 (0)