From 4c9a7d5840562a07f6f98191041279231495c94f Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 31 May 2023 16:10:01 +0200 Subject: [PATCH] fix(logger): print unhandled errors before summary (#3474) --- packages/vitest/src/node/reporters/base.ts | 19 ++++++++++++------- packages/vitest/src/node/reporters/basic.ts | 4 ++-- .../node/reporters/benchmark/table/index.ts | 2 +- packages/vitest/src/node/reporters/default.ts | 2 +- packages/vitest/src/utils/tasks.ts | 2 +- .../test/__snapshots__/runner.test.ts.snap | 8 +------- 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/packages/vitest/src/node/reporters/base.ts b/packages/vitest/src/node/reporters/base.ts index 2a55523f19ee..a5aad35b86f2 100644 --- a/packages/vitest/src/node/reporters/base.ts +++ b/packages/vitest/src/node/reporters/base.ts @@ -51,11 +51,10 @@ export abstract class BaseReporter implements Reporter { async onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) { this.end = performance.now() - await this.reportSummary(files) + await this.reportSummary(files, errors) if (errors.length) { if (!this.ctx.config.dangerouslyIgnoreUnhandledErrors) process.exitCode = 1 - await this.ctx.logger.printUnhandledErrors(errors) } } @@ -208,15 +207,15 @@ export abstract class BaseReporter implements Reporter { ))) } - async reportSummary(files: File[]) { - await this.printErrorsSummary(files) + async reportSummary(files: File[], errors: unknown[]) { + await this.printErrorsSummary(files, errors) if (this.mode === 'benchmark') await this.reportBenchmarkSummary(files) else - await this.reportTestSummary(files) + await this.reportTestSummary(files, errors) } - async reportTestSummary(files: File[]) { + async reportTestSummary(files: File[], errors: unknown[]) { const tests = getTests(files) const logger = this.ctx.logger @@ -262,6 +261,8 @@ export abstract class BaseReporter implements Reporter { const failed = tests.filter(t => t.meta?.typecheck && t.result?.errors?.length) logger.log(padTitle('Type Errors'), failed.length ? c.bold(c.red(`${failed.length} failed`)) : c.dim('no errors')) } + if (errors.length) + logger.log(padTitle('Errors'), c.bold(c.red(`${errors.length} error${errors.length > 1 ? 's' : ''}`))) logger.log(padTitle('Start at'), formatTimeString(this._timeStart)) if (this.watchFilters) logger.log(padTitle('Duration'), time(threadTime)) @@ -273,7 +274,7 @@ export abstract class BaseReporter implements Reporter { logger.log() } - private async printErrorsSummary(files: File[]) { + private async printErrorsSummary(files: File[], errors: unknown[]) { const logger = this.ctx.logger const suites = getSuites(files) const tests = getTests(files) @@ -298,6 +299,10 @@ export abstract class BaseReporter implements Reporter { await this.printTaskErrors(failedTests, errorDivider) } + if (errors.length) { + await logger.printUnhandledErrors(errors) + logger.error() + } return tests } diff --git a/packages/vitest/src/node/reporters/basic.ts b/packages/vitest/src/node/reporters/basic.ts index e67e31cdc760..a6b2b3632a90 100644 --- a/packages/vitest/src/node/reporters/basic.ts +++ b/packages/vitest/src/node/reporters/basic.ts @@ -4,9 +4,9 @@ import { BaseReporter } from './base' export class BasicReporter extends BaseReporter { isTTY = false - reportSummary(files: File[]) { + reportSummary(files: File[], errors: unknown[]) { // non-tty mode doesn't add a new line this.ctx.logger.log() - return super.reportSummary(files) + return super.reportSummary(files, errors) } } diff --git a/packages/vitest/src/node/reporters/benchmark/table/index.ts b/packages/vitest/src/node/reporters/benchmark/table/index.ts index fb599bc02234..bcff43acfebb 100644 --- a/packages/vitest/src/node/reporters/benchmark/table/index.ts +++ b/packages/vitest/src/node/reporters/benchmark/table/index.ts @@ -14,7 +14,7 @@ export class TableReporter extends BaseReporter { const files = this.ctx.state.getFiles(this.watchFilters) createTableRenderer(files, this.rendererOptions).stop() this.ctx.logger.log() - await super.reportSummary(files) + await super.reportSummary(files, this.ctx.state.getUnhandledErrors()) super.onWatcherStart() } diff --git a/packages/vitest/src/node/reporters/default.ts b/packages/vitest/src/node/reporters/default.ts index e94cb8f86883..97d0c37a00e7 100644 --- a/packages/vitest/src/node/reporters/default.ts +++ b/packages/vitest/src/node/reporters/default.ts @@ -14,7 +14,7 @@ export class DefaultReporter extends BaseReporter { const files = this.ctx.state.getFiles(this.watchFilters) createListRenderer(files, this.rendererOptions).stop() this.ctx.logger.log() - await super.reportSummary(files) + await super.reportSummary(files, this.ctx.state.getUnhandledErrors()) super.onWatcherStart() } diff --git a/packages/vitest/src/utils/tasks.ts b/packages/vitest/src/utils/tasks.ts index 762d084b07ab..96620cd73127 100644 --- a/packages/vitest/src/utils/tasks.ts +++ b/packages/vitest/src/utils/tasks.ts @@ -10,7 +10,7 @@ export function hasBenchmark(suite: Arrayable): boolean { export function hasFailedSnapshot(suite: Arrayable): boolean { return getTests(suite).some((s) => { - return s.result?.errors?.some(e => e && e.message && e.message.match(/Snapshot .* mismatched/)) + return s.result?.errors?.some(e => typeof e?.message === 'string' && e.message.match(/Snapshot .* mismatched/)) }) } diff --git a/test/typescript/test/__snapshots__/runner.test.ts.snap b/test/typescript/test/__snapshots__/runner.test.ts.snap index ff543a322695..4bf1a9c8e894 100644 --- a/test/typescript/test/__snapshots__/runner.test.ts.snap +++ b/test/typescript/test/__snapshots__/runner.test.ts.snap @@ -94,14 +94,8 @@ Breaking changes might not follow semver, please pin Vitest's version when using ⎯⎯⎯⎯⎯⎯ Typecheck Error ⎯⎯⎯⎯⎯⎯⎯ Error: error TS18003: No inputs were found in config file '/tsconfig.vitest-temp.json'. Specified 'include' paths were '[\\"src\\"]' and 'exclude' paths were '[\\"**/dist/**\\",\\"./dist\\"]'. -" -`; -exports[`should fail > typechecks with custom tsconfig 1`] = ` -"TypeCheckError: Expected 1 arguments, but got 0. -TypeCheckError: Expected 1 arguments, but got 0. -TypeCheckError: Expected 1 arguments, but got 0. -TypeCheckError: Expected 1 arguments, but got 0." +" `; exports[`should fail > typecheks with custom tsconfig 1`] = `