Skip to content

Commit

Permalink
fix(vitest): fix tap reporter to handle custom error (#4897)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Jan 8, 2024
1 parent 55f5349 commit f8ba80f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/vitest/src/node/reporters/tap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Task } from '@vitest/runner'
import type { ParsedStack } from '@vitest/utils'
import type { ErrorWithDiff, ParsedStack } from '@vitest/utils'
import type { Vitest } from '../../node'
import type { Reporter } from '../../types/reporter'
import { parseErrorStacktrace } from '../../utils/source-map'
Expand Down Expand Up @@ -36,9 +36,10 @@ export class TapReporter implements Reporter {
return ''
}

private logErrorDetails(error: Error, stack?: ParsedStack) {
this.logger.log(`name: ${yamlString(error.name)}`)
this.logger.log(`message: ${yamlString(error.message)}`)
private logErrorDetails(error: ErrorWithDiff, stack?: ParsedStack) {
const errorName = error.name || error.nameStr || 'Unknown Error'
this.logger.log(`name: ${yamlString(String(errorName))}`)
this.logger.log(`message: ${yamlString(String(error.message))}`)

if (stack) {
// For compatibility with tap-mocha-reporter
Expand Down
17 changes: 17 additions & 0 deletions test/reporters/fixtures/custom-error/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { test } from 'vitest';

test('no name object', () => {
throw { noName: 'hi' };
});

test('string', () => {
throw "hi";
});

test('number', () => {
throw 1234;
});

test('number name object', () => {
throw { name: 1234 };
});
3 changes: 3 additions & 0 deletions test/reporters/fixtures/custom-error/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
37 changes: 37 additions & 0 deletions test/reporters/tests/tap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

test('handle custom error without name', async () => {
let { stdout, stderr } = await runVitest({ reporters: 'tap-flat', root: './fixtures/custom-error' })
stdout = stdout.replaceAll(/time=(\S*)/g, 'time=[...]') // strip non-deterministic output
expect(stdout).toMatchInlineSnapshot(`
"TAP version 13
1..4
not ok 1 - basic.test.ts > no name object # time=[...]
---
error:
name: "Unknown Error"
message: "undefined"
...
not ok 2 - basic.test.ts > string # time=[...]
---
error:
name: "Unknown Error"
message: "hi"
...
not ok 3 - basic.test.ts > number # time=[...]
---
error:
name: "Unknown Error"
message: "1234"
...
not ok 4 - basic.test.ts > number name object # time=[...]
---
error:
name: "1234"
message: "undefined"
...
"
`)
expect(stderr).toBe('')
})

0 comments on commit f8ba80f

Please sign in to comment.