Skip to content

Commit

Permalink
fix(logger): print unhandled errors before summary (#3474)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored May 31, 2023
1 parent add29c8 commit 4c9a7d5
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 19 deletions.
19 changes: 12 additions & 7 deletions packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/reporters/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/reporters/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/utils/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function hasBenchmark(suite: Arrayable<Suite>): boolean {

export function hasFailedSnapshot(suite: Arrayable<Task>): 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/))
})
}

Expand Down
8 changes: 1 addition & 7 deletions test/typescript/test/__snapshots__/runner.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<root>/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`] = `
Expand Down

0 comments on commit 4c9a7d5

Please sign in to comment.