GeezerVison is showing github.com — fix: `dangerouslyIgnoreUnhandledErrors` without base reporter (#6808) · vitest-dev/vitest@0bf0ab0
File treeExpand file treeCollapse file tree Open diff view settings fixtures/dangerously-ignore-unhandled-errors/tests
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/node/core.ts +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -352,6 +352,7 @@ export class Vitest { | 352 | 352 | process.exitCode = 1 | 353 | 353 | } | 354 | 354 |
| | 355 | + this.checkUnhandledErrors(errors) | 355 | 356 | await this.report('onFinished', files, errors) | 356 | 357 | await this.initCoverageProvider() | 357 | 358 | await this.coverageProvider?.mergeReports?.(coverages) | @@ -613,9 +614,11 @@ export class Vitest { | 613 | 614 | .finally(async () => { | 614 | 615 | // can be duplicate files if different projects are using the same file | 615 | 616 | const files = Array.from(new Set(specs.map(spec => spec.moduleId))) | | 617 | + const errors = this.state.getUnhandledErrors() | 616 | 618 | const coverage = await this.coverageProvider?.generateCoverage({ allTestsRun }) | 617 | 619 |
| 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) | 619 | 622 | await this.reportCoverage(coverage, allTestsRun) | 620 | 623 |
| 621 | 624 | this.runningPromise = undefined | @@ -896,6 +899,12 @@ export class Vitest { | 896 | 899 | } | 897 | 900 | } | 898 | 901 |
| | 902 | + checkUnhandledErrors(errors: unknown[]) { | | 903 | + if (errors.length && !this.config.dangerouslyIgnoreUnhandledErrors) { | | 904 | + process.exitCode = 1 | | 905 | + } | | 906 | + } | | 907 | + | 899 | 908 | private unregisterWatcher = noop | 900 | 909 | private registerWatcher() { | 901 | 910 | const watcher = this.server.watcher | |
Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/node/pools/rpc.ts +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -94,7 +94,10 @@ export function createMethodsRPC(project: WorkspaceProject, options: MethodsOpti | 94 | 94 | ctx.state.catchError(err, type) | 95 | 95 | }, | 96 | 96 | 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) | 98 | 101 | }, | 99 | 102 | onCancel(reason) { | 100 | 103 | ctx.cancelCurrentRun(reason) | |
Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/node/reporters/base.ts -5Lines changed: 0 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -90,11 +90,6 @@ export abstract class BaseReporter implements Reporter { | 90 | 90 | this.end = performance.now() | 91 | 91 |
| 92 | 92 | this.reportSummary(files, errors) | 93 | | - if (errors.length) { | 94 | | - if (!this.ctx.config.dangerouslyIgnoreUnhandledErrors) { | 95 | | - process.exitCode = 1 | 96 | | - } | 97 | | - } | 98 | 93 | } | 99 | 94 |
| 100 | 95 | onTaskUpdate(packs: TaskResultPack[]) { | |
Collapse file Copy file name to clipboard +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { test } from "vitest" | | 2 | + | | 3 | +test("Some test", () => { | | 4 | + // | | 5 | +}) | | 6 | + | | 7 | +new Promise((_, reject) => reject(new Error("intentional unhandled error"))) |
Collapse file Copy file name to clipboard +35Lines changed: 35 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +}) |
|