[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix: use `toJSON` for error serialization (#5526)

authored by

Hiroshi Ogawa and committed by
GitHub
(May 2, 2024, 11:01 AM +0200) 19a21e49 c9883f3e

+14 -3
+2
packages/utils/src/error.ts
··· 44 44 return val.tagName 45 45 if (typeof val.asymmetricMatch === 'function') 46 46 return `${val.toString()} ${format(val.sample)}` 47 + if (typeof val.toJSON === 'function') 48 + return val.toJSON() 47 49 48 50 if (seen.has(val)) 49 51 return seen.get(val)
+5
test/reporters/fixtures/error-to-json.test.ts
··· 1 + import { test } from "vitest"; 2 + 3 + test("error serialization with toJSON", () => { 4 + throw Object.assign(new Error("hello"), { date: new Date(0) }) 5 + })
+7
test/reporters/tests/error-to-json.test.ts
··· 1 + import { expect, test } from 'vitest' 2 + import { runVitest } from '../../test-utils' 3 + 4 + test('should print logs correctly', async () => { 5 + const result = await runVitest({ root: './fixtures' }, ['error-to-json.test.ts']) 6 + expect(result.stderr).toContain(`Serialized Error: { date: '1970-01-01T00:00:00.000Z' }`) 7 + })
-3
test/reporters/tests/__snapshots__/html.test.ts.snap
··· 45 45 "errors": [ 46 46 { 47 47 "actual": "2", 48 - "constructor": "Function<AssertionError>", 49 48 "diff": "- Expected 50 49 + Received 51 50 ··· 59 58 "showDiff": true, 60 59 "stack": "AssertionError: expected 2 to deeply equal 1", 61 60 "stackStr": "AssertionError: expected 2 to deeply equal 1", 62 - "toJSON": "Function<anonymous>", 63 - "toString": "Function<toString>", 64 61 }, 65 62 ], 66 63 "hooks": {