Skip to content

Commit

Permalink
fix: dangerouslyIgnoreUnhandledErrors without base reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Oct 29, 2024
1 parent 47d7c0a commit 473d377
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
11 changes: 10 additions & 1 deletion packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ export class Vitest {
process.exitCode = 1
}

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

await this.report('onFinished', this.state.getFiles(files), this.state.getUnhandledErrors(), coverage)
this.checkUnhandledErrors(errors)
await this.report('onFinished', this.state.getFiles(files), errors, coverage)
await this.reportCoverage(coverage, allTestsRun)

this.runningPromise = undefined
Expand Down Expand Up @@ -896,6 +899,12 @@ export class Vitest {
}
}

checkUnhandledErrors(errors: unknown[]) {
if (errors.length && !this.config.dangerouslyIgnoreUnhandledErrors) {
process.exitCode = 1
}
}

private unregisterWatcher = noop
private registerWatcher() {
const watcher = this.server.watcher
Expand Down
5 changes: 4 additions & 1 deletion packages/vitest/src/node/pools/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ export function createMethodsRPC(project: WorkspaceProject, options: MethodsOpti
ctx.state.catchError(err, type)
},
onFinished(files) {
return ctx.report('onFinished', files, ctx.state.getUnhandledErrors())
const errors = ctx.state.getUnhandledErrors()
ctx.checkUnhandledErrors(errors)

return ctx.report('onFinished', files, errors)
},
onCancel(reason) {
ctx.cancelCurrentRun(reason)
Expand Down
5 changes: 0 additions & 5 deletions packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ export abstract class BaseReporter implements Reporter {
this.end = performance.now()

this.reportSummary(files, errors)
if (errors.length) {
if (!this.ctx.config.dangerouslyIgnoreUnhandledErrors) {
process.exitCode = 1
}
}
}

onTaskUpdate(packs: TaskResultPack[]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from "vitest"

test("Some test", () => {
//
})

new Promise((_, reject) => reject(new Error("intentional unhandled error")))
35 changes: 35 additions & 0 deletions test/config/test/dangerously-ignore-unhandled-errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect, test } from 'vitest'

import { runVitest } from '../../test-utils'

test('{ dangerouslyIgnoreUnhandledErrors: true }', async () => {
const { stderr, stdout, exitCode } = await runVitest({
root: 'fixtures/dangerously-ignore-unhandled-errors',
dangerouslyIgnoreUnhandledErrors: true,
})

expect(exitCode).toBe(0)
expect(stdout).toMatch('Vitest caught 1 unhandled error during the test run')
expect(stderr).toMatch('Error: intentional unhandled error')
})

test('{ dangerouslyIgnoreUnhandledErrors: true } without reporter', async () => {
const { exitCode } = await runVitest({
root: 'fixtures/dangerously-ignore-unhandled-errors',
dangerouslyIgnoreUnhandledErrors: true,
reporters: [{ onInit: () => {} }],
})

expect(exitCode).toBe(0)
})

test('{ dangerouslyIgnoreUnhandledErrors: false }', async () => {
const { stderr, stdout, exitCode } = await runVitest({
root: 'fixtures/dangerously-ignore-unhandled-errors',
dangerouslyIgnoreUnhandledErrors: false,
})

expect(exitCode).toBe(1)
expect(stdout).toMatch('Vitest caught 1 unhandled error during the test run')
expect(stderr).toMatch('Error: intentional unhandled error')
})

0 comments on commit 473d377

Please sign in to comment.