GeezerVison is showing github.com — fix: capture `unhandledRejection` even when base reporter is not used… · vitest-dev/vitest@8878b04
File treeExpand file treeCollapse file tree Open diff view settings fixtures/unhandled-rejections
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/node/logger.ts +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,6 +46,7 @@ export class Logger { | 46 | 46 | this.console = new Console({ stdout: outputStream, stderr: errorStream }) | 47 | 47 | this.logUpdate = createLogUpdate(this.outputStream) | 48 | 48 | this._highlights.clear() | | 49 | + this.registerUnhandledRejection() | 49 | 50 | } | 50 | 51 |
| 51 | 52 | log(...args: any[]) { | @@ -317,4 +318,24 @@ export class Logger { | 317 | 318 | }) | 318 | 319 | this.log(c.red(divider())) | 319 | 320 | } | | 321 | + | | 322 | + private registerUnhandledRejection() { | | 323 | + const onUnhandledRejection = (err: unknown) => { | | 324 | + process.exitCode = 1 | | 325 | + | | 326 | + this.printError(err, { | | 327 | + fullStack: true, | | 328 | + type: 'Unhandled Rejection', | | 329 | + }) | | 330 | + | | 331 | + this.error('\n\n') | | 332 | + process.exit() | | 333 | + } | | 334 | + | | 335 | + process.on('unhandledRejection', onUnhandledRejection) | | 336 | + | | 337 | + this.ctx.onClose(() => { | | 338 | + process.off('unhandledRejection', onUnhandledRejection) | | 339 | + }) | | 340 | + } | 320 | 341 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/node/reporters/base.ts -21Lines changed: 0 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,11 +59,9 @@ export abstract class BaseReporter implements Reporter { | 59 | 59 | private _lastRunTimer: NodeJS.Timeout | undefined | 60 | 60 | private _lastRunCount = 0 | 61 | 61 | private _timeStart = new Date() | 62 | | - private _offUnhandledRejection?: () => void | 63 | 62 |
| 64 | 63 | constructor(options: BaseOptions = {}) { | 65 | 64 | this.isTTY = options.isTTY ?? ((isNode || isDeno) && process.stdout?.isTTY && !isCI) | 66 | | - this.registerUnhandledRejection() | 67 | 65 | } | 68 | 66 |
| 69 | 67 | get mode() { | @@ -72,9 +70,6 @@ export abstract class BaseReporter implements Reporter { | 72 | 70 |
| 73 | 71 | onInit(ctx: Vitest) { | 74 | 72 | this.ctx = ctx | 75 | | - ctx.onClose(() => { | 76 | | - this._offUnhandledRejection?.() | 77 | | - }) | 78 | 73 | ctx.logger.printBanner() | 79 | 74 | this.start = performance.now() | 80 | 75 | } | @@ -626,22 +621,6 @@ export abstract class BaseReporter implements Reporter { | 626 | 621 | errorDivider() | 627 | 622 | } | 628 | 623 | } | 629 | | - | 630 | | - registerUnhandledRejection() { | 631 | | - const onUnhandledRejection = async (err: unknown) => { | 632 | | - process.exitCode = 1 | 633 | | - this.ctx.logger.printError(err, { | 634 | | - fullStack: true, | 635 | | - type: 'Unhandled Rejection', | 636 | | - }) | 637 | | - this.ctx.logger.error('\n\n') | 638 | | - process.exit() | 639 | | - } | 640 | | - process.on('unhandledRejection', onUnhandledRejection) | 641 | | - this._offUnhandledRejection = () => { | 642 | | - process.off('unhandledRejection', onUnhandledRejection) | 643 | | - } | 644 | | - } | 645 | 624 | } | 646 | 625 |
| 647 | 626 | function padTitle(str: string) { | |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +export function setup() { | | 2 | + void new Promise((_, reject) => reject(new Error('intentional unhandled rejection'))) | | 3 | +} |
Collapse file Copy file name to clipboard +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { test } from "vitest" | | 2 | + | | 3 | +test("Some test", () => {}) | | 4 | + |
Collapse file Copy file name to clipboard +16Lines changed: 16 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('unhandled rejections of main thread are reported even when no reporter is used', async () => { | | 6 | + const { stderr, exitCode } = await runVitest({ | | 7 | + root: 'fixtures/unhandled-rejections', | | 8 | + globalSetup: ['setup-unhandled-rejections.ts'], | | 9 | + reporters: [{ onInit: () => {} }], | | 10 | + }) | | 11 | + | | 12 | + expect(exitCode).toBe(1) | | 13 | + expect(stderr).toContain('Unhandled Rejection') | | 14 | + expect(stderr).toContain('Error: intentional unhandled rejection') | | 15 | + expect(stderr).toContain('setup-unhandled-rejections.ts:2:42') | | 16 | +}) |
|