Skip to content

Commit

Permalink
feat: pretty display unhandled rejection
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 22, 2022
1 parent af17530 commit 518e3dd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/vitest/src/node/cli-api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { execa } from 'execa'
import type { UserConfig as ViteUserConfig } from 'vite'
import c from 'picocolors'
import type { UserConfig } from '../types'
import { ensurePackageInstalled } from '../utils'
import { createVitest } from './create'
import { registerConsoleShortcuts } from './stdin'
import { printError } from './reporters/renderers/diff'
import { divider } from './reporters/renderers/utils'

export interface CliOptions extends UserConfig {
/**
Expand Down Expand Up @@ -66,7 +69,10 @@ export async function startVitest(cliFilters: string[], options: CliOptions, vit
}
catch (e) {
process.exitCode = 1
throw e
ctx.error(`\n${c.red(divider(c.bold(c.inverse(' Unhandled Error '))))}`)
await printError(e, ctx)
ctx.error('\n\n')
return false
}
finally {
if (!ctx.config.watch)
Expand Down
14 changes: 14 additions & 0 deletions packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export abstract class BaseReporter implements Reporter {
isTTY = process.stdout.isTTY && !process.env.CI
ctx: Vitest = undefined!

constructor() {
this.registerUnhandledRejection()
}

onInit(ctx: Vitest) {
this.ctx = ctx

Expand Down Expand Up @@ -182,4 +186,14 @@ export abstract class BaseReporter implements Reporter {
errorDivider()
}
}

registerUnhandledRejection() {
process.on('unhandledRejection', async(err) => {
process.exitCode = 1
this.ctx.error(`\n${c.red(divider(c.bold(c.inverse(' Unhandled Rejection '))))}`)
await printError(err, this.ctx)
this.ctx.error('\n\n')
process.exit(1)
})
}
}

0 comments on commit 518e3dd

Please sign in to comment.