Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show actual unhandled errors, serialize DOMErrors #2253

Merged
merged 3 commits into from Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/vitest/src/node/error.ts
Expand Up @@ -91,8 +91,6 @@ function printErrorType(type: string, ctx: Vitest) {
}

const skipErrorProperties = [
'message',
'name',
'nameStr',
'stack',
'cause',
Expand All @@ -102,8 +100,8 @@ const skipErrorProperties = [
'showDiff',
'actual',
'expected',
'constructor',
'toString',
...Object.getOwnPropertyNames(Error.prototype),
...Object.getOwnPropertyNames(Object.prototype),
]

function getErrorProperties(e: ErrorWithDiff) {
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/error.ts
Expand Up @@ -53,10 +53,10 @@ export function serializeError(val: any, seen = new WeakMap()): any {
let obj = val
while (obj && obj !== OBJECT_PROTO) {
Object.getOwnPropertyNames(obj).forEach((key) => {
if ((key in clone))
if (key in clone)
return
try {
clone[key] = serializeError(obj[key], seen)
clone[key] = serializeError(val[key], seen)
}
catch (err) {
// delete in case it has a setter from prototype that might throw
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/runtime/worker.ts
Expand Up @@ -8,6 +8,7 @@ import { getWorkerState } from '../utils'
import type { MockMap } from '../types/mocker'
import { executeInViteNode } from './execute'
import { rpc } from './rpc'
import { processError } from './error'

let _viteNode: {
run: (files: string[], config: ResolvedConfig) => Promise<void>
Expand All @@ -32,7 +33,7 @@ async function startViteNode(ctx: WorkerContext) {
}

process.on('unhandledRejection', (err) => {
rpc().onUnhandledRejection(err)
rpc().onUnhandledRejection(processError(err))
})

const { config } = ctx
Expand Down
12 changes: 12 additions & 0 deletions test/core/test/serialize.test.ts
@@ -1,3 +1,5 @@
// @vitest-environment jsdom

import { describe, expect, it } from 'vitest'
import { serializeError } from '../../../packages/vitest/src/runtime/error'

Expand Down Expand Up @@ -125,4 +127,14 @@ describe('error serialize', () => {
unserializable: '<unserializable>: I am unserializable',
})
})

it('can serialize DOMException', () => {
const err = new DOMException('You failed', 'InvalidStateError')
expect(serializeError(err)).toMatchObject({
NETWORK_ERR: 19,
name: 'InvalidStateError',
message: 'You failed',
stack: expect.stringContaining('InvalidStateError: You failed'),
})
})
})