fix(reporter): report test module if it failed to run (#9272) · vitest-dev/vitest@c788829

2 files changed

lines changed

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

}

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+

})