Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dangerouslyIgnoreUnhandledErrors without base reporter #6808

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
})