Skip to content

Commit 4c9a7d5

Browse files
authored
fix(logger): print unhandled errors before summary (#3474)
1 parent add29c8 commit 4c9a7d5

File tree

6 files changed

+18
-19
lines changed

6 files changed

+18
-19
lines changed

packages/vitest/src/node/reporters/base.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ export abstract class BaseReporter implements Reporter {
5151
async onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
5252
this.end = performance.now()
5353

54-
await this.reportSummary(files)
54+
await this.reportSummary(files, errors)
5555
if (errors.length) {
5656
if (!this.ctx.config.dangerouslyIgnoreUnhandledErrors)
5757
process.exitCode = 1
58-
await this.ctx.logger.printUnhandledErrors(errors)
5958
}
6059
}
6160

@@ -208,15 +207,15 @@ export abstract class BaseReporter implements Reporter {
208207
)))
209208
}
210209

211-
async reportSummary(files: File[]) {
212-
await this.printErrorsSummary(files)
210+
async reportSummary(files: File[], errors: unknown[]) {
211+
await this.printErrorsSummary(files, errors)
213212
if (this.mode === 'benchmark')
214213
await this.reportBenchmarkSummary(files)
215214
else
216-
await this.reportTestSummary(files)
215+
await this.reportTestSummary(files, errors)
217216
}
218217

219-
async reportTestSummary(files: File[]) {
218+
async reportTestSummary(files: File[], errors: unknown[]) {
220219
const tests = getTests(files)
221220
const logger = this.ctx.logger
222221

@@ -262,6 +261,8 @@ export abstract class BaseReporter implements Reporter {
262261
const failed = tests.filter(t => t.meta?.typecheck && t.result?.errors?.length)
263262
logger.log(padTitle('Type Errors'), failed.length ? c.bold(c.red(`${failed.length} failed`)) : c.dim('no errors'))
264263
}
264+
if (errors.length)
265+
logger.log(padTitle('Errors'), c.bold(c.red(`${errors.length} error${errors.length > 1 ? 's' : ''}`)))
265266
logger.log(padTitle('Start at'), formatTimeString(this._timeStart))
266267
if (this.watchFilters)
267268
logger.log(padTitle('Duration'), time(threadTime))
@@ -273,7 +274,7 @@ export abstract class BaseReporter implements Reporter {
273274
logger.log()
274275
}
275276

276-
private async printErrorsSummary(files: File[]) {
277+
private async printErrorsSummary(files: File[], errors: unknown[]) {
277278
const logger = this.ctx.logger
278279
const suites = getSuites(files)
279280
const tests = getTests(files)
@@ -298,6 +299,10 @@ export abstract class BaseReporter implements Reporter {
298299

299300
await this.printTaskErrors(failedTests, errorDivider)
300301
}
302+
if (errors.length) {
303+
await logger.printUnhandledErrors(errors)
304+
logger.error()
305+
}
301306
return tests
302307
}
303308

packages/vitest/src/node/reporters/basic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { BaseReporter } from './base'
44
export class BasicReporter extends BaseReporter {
55
isTTY = false
66

7-
reportSummary(files: File[]) {
7+
reportSummary(files: File[], errors: unknown[]) {
88
// non-tty mode doesn't add a new line
99
this.ctx.logger.log()
10-
return super.reportSummary(files)
10+
return super.reportSummary(files, errors)
1111
}
1212
}

packages/vitest/src/node/reporters/benchmark/table/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class TableReporter extends BaseReporter {
1414
const files = this.ctx.state.getFiles(this.watchFilters)
1515
createTableRenderer(files, this.rendererOptions).stop()
1616
this.ctx.logger.log()
17-
await super.reportSummary(files)
17+
await super.reportSummary(files, this.ctx.state.getUnhandledErrors())
1818
super.onWatcherStart()
1919
}
2020

packages/vitest/src/node/reporters/default.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class DefaultReporter extends BaseReporter {
1414
const files = this.ctx.state.getFiles(this.watchFilters)
1515
createListRenderer(files, this.rendererOptions).stop()
1616
this.ctx.logger.log()
17-
await super.reportSummary(files)
17+
await super.reportSummary(files, this.ctx.state.getUnhandledErrors())
1818
super.onWatcherStart()
1919
}
2020

packages/vitest/src/utils/tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function hasBenchmark(suite: Arrayable<Suite>): boolean {
1010

1111
export function hasFailedSnapshot(suite: Arrayable<Task>): boolean {
1212
return getTests(suite).some((s) => {
13-
return s.result?.errors?.some(e => e && e.message && e.message.match(/Snapshot .* mismatched/))
13+
return s.result?.errors?.some(e => typeof e?.message === 'string' && e.message.match(/Snapshot .* mismatched/))
1414
})
1515
}
1616

test/typescript/test/__snapshots__/runner.test.ts.snap

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,8 @@ Breaking changes might not follow semver, please pin Vitest's version when using
9494
⎯⎯⎯⎯⎯⎯ Typecheck Error ⎯⎯⎯⎯⎯⎯⎯
9595
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\\"]'.
9696
97-
"
98-
`;
9997
100-
exports[`should fail > typechecks with custom tsconfig 1`] = `
101-
"TypeCheckError: Expected 1 arguments, but got 0.
102-
TypeCheckError: Expected 1 arguments, but got 0.
103-
TypeCheckError: Expected 1 arguments, but got 0.
104-
TypeCheckError: Expected 1 arguments, but got 0."
98+
"
10599
`;
106100
107101
exports[`should fail > typecheks with custom tsconfig 1`] = `

0 commit comments

Comments
 (0)