Skip to content

Commit 0bf0ab0

Browse files
authored
fix: dangerouslyIgnoreUnhandledErrors without base reporter (#6808)
1 parent bcc5908 commit 0bf0ab0

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

packages/vitest/src/node/core.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ export class Vitest {
352352
process.exitCode = 1
353353
}
354354

355+
this.checkUnhandledErrors(errors)
355356
await this.report('onFinished', files, errors)
356357
await this.initCoverageProvider()
357358
await this.coverageProvider?.mergeReports?.(coverages)
@@ -613,9 +614,11 @@ export class Vitest {
613614
.finally(async () => {
614615
// can be duplicate files if different projects are using the same file
615616
const files = Array.from(new Set(specs.map(spec => spec.moduleId)))
617+
const errors = this.state.getUnhandledErrors()
616618
const coverage = await this.coverageProvider?.generateCoverage({ allTestsRun })
617619

618-
await this.report('onFinished', this.state.getFiles(files), this.state.getUnhandledErrors(), coverage)
620+
this.checkUnhandledErrors(errors)
621+
await this.report('onFinished', this.state.getFiles(files), errors, coverage)
619622
await this.reportCoverage(coverage, allTestsRun)
620623

621624
this.runningPromise = undefined
@@ -896,6 +899,12 @@ export class Vitest {
896899
}
897900
}
898901

902+
checkUnhandledErrors(errors: unknown[]) {
903+
if (errors.length && !this.config.dangerouslyIgnoreUnhandledErrors) {
904+
process.exitCode = 1
905+
}
906+
}
907+
899908
private unregisterWatcher = noop
900909
private registerWatcher() {
901910
const watcher = this.server.watcher

packages/vitest/src/node/pools/rpc.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ export function createMethodsRPC(project: WorkspaceProject, options: MethodsOpti
9494
ctx.state.catchError(err, type)
9595
},
9696
onFinished(files) {
97-
return ctx.report('onFinished', files, ctx.state.getUnhandledErrors())
97+
const errors = ctx.state.getUnhandledErrors()
98+
ctx.checkUnhandledErrors(errors)
99+
100+
return ctx.report('onFinished', files, errors)
98101
},
99102
onCancel(reason) {
100103
ctx.cancelCurrentRun(reason)

packages/vitest/src/node/reporters/base.ts

-5
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ export abstract class BaseReporter implements Reporter {
9090
this.end = performance.now()
9191

9292
this.reportSummary(files, errors)
93-
if (errors.length) {
94-
if (!this.ctx.config.dangerouslyIgnoreUnhandledErrors) {
95-
process.exitCode = 1
96-
}
97-
}
9893
}
9994

10095
onTaskUpdate(packs: TaskResultPack[]) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from "vitest"
2+
3+
test("Some test", () => {
4+
//
5+
})
6+
7+
new Promise((_, reject) => reject(new Error("intentional unhandled error")))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { expect, test } from 'vitest'
2+
3+
import { runVitest } from '../../test-utils'
4+
5+
test('{ dangerouslyIgnoreUnhandledErrors: true }', async () => {
6+
const { stderr, stdout, exitCode } = await runVitest({
7+
root: 'fixtures/dangerously-ignore-unhandled-errors',
8+
dangerouslyIgnoreUnhandledErrors: true,
9+
})
10+
11+
expect(exitCode).toBe(0)
12+
expect(stdout).toMatch('Vitest caught 1 unhandled error during the test run')
13+
expect(stderr).toMatch('Error: intentional unhandled error')
14+
})
15+
16+
test('{ dangerouslyIgnoreUnhandledErrors: true } without reporter', async () => {
17+
const { exitCode } = await runVitest({
18+
root: 'fixtures/dangerously-ignore-unhandled-errors',
19+
dangerouslyIgnoreUnhandledErrors: true,
20+
reporters: [{ onInit: () => {} }],
21+
})
22+
23+
expect(exitCode).toBe(0)
24+
})
25+
26+
test('{ dangerouslyIgnoreUnhandledErrors: false }', async () => {
27+
const { stderr, stdout, exitCode } = await runVitest({
28+
root: 'fixtures/dangerously-ignore-unhandled-errors',
29+
dangerouslyIgnoreUnhandledErrors: false,
30+
})
31+
32+
expect(exitCode).toBe(1)
33+
expect(stdout).toMatch('Vitest caught 1 unhandled error during the test run')
34+
expect(stderr).toMatch('Error: intentional unhandled error')
35+
})

0 commit comments

Comments
 (0)