Skip to content

Commit

Permalink
fix: show actual unhandled errors, serialize DOMErrors (#2253)
Browse files Browse the repository at this point in the history
* fix: show actual unhandled errors, serialize DOMErrors

* chore: val -> err

* chore: simplify access
  • Loading branch information
sheremet-va committed Nov 1, 2022
1 parent 41361fa commit 7b78931
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
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'),
})
})
})

0 comments on commit 7b78931

Please sign in to comment.