forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialize.test.ts
171 lines (152 loc) · 4.53 KB
/
serialize.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// @vitest-environment jsdom
import { describe, expect, it } from 'vitest'
import { serializeError } from '../../../packages/vitest/src/runtime/error'
describe('error serialize', () => {
it('works', () => {
expect(serializeError(undefined)).toEqual(undefined)
expect(serializeError(null)).toEqual(null)
expect(serializeError('hi')).toEqual('hi')
expect(serializeError({
foo: 'hi',
promise: new Promise(() => {}),
fn: () => {},
null: null,
symbol: Symbol('hi'),
nested: {
false: false,
class: class {},
},
// Intentionally test with a sparse array to verify it remains sparse during serialization.
// eslint-disable-next-line no-sparse-arrays
array: [1,, 3],
})).toMatchSnapshot()
})
it('Should skip circular references to prevent hit the call stack limit', () => {
const error: Record<string, any> = {
toString: () => {
return 'ops something went wrong'
},
}
error.whatever = error
error.whateverArray = [error, error]
error.whateverArrayClone = error.whateverArray
expect(serializeError(error)).toMatchSnapshot()
})
it('Should handle object with getter/setter correctly', () => {
const user = {
name: 'John',
surname: 'Smith',
get fullName() {
return `${this.name} ${this.surname}`
},
set fullName(value) {
[this.name, this.surname] = value.split(' ')
},
}
expect(serializeError(user)).toEqual({
name: 'John',
surname: 'Smith',
fullName: 'John Smith',
})
})
it('Should copy the full prototype chain including non-enumerable properties', () => {
const user = {
name: 'John',
surname: 'Smith',
}
Object.setPrototypeOf(user, {
name: 'Mr',
base: true,
})
Object.defineProperty(user, 'fullName', { enumerable: false, value: 'John Smith' })
const serialized = serializeError(user)
expect(serialized).not.toBe(user)
expect(serialized).toEqual({
name: 'John',
surname: 'Smith',
fullName: 'John Smith',
base: true,
})
})
it('Should not retain the constructor of an object', () => {
// https://github.com/vitest-dev/vitest/issues/374
// Objects with `Error` constructors appear to cause problems during worker communication using
// `MessagePort`, so the serialized error object should have been recreated as plain object.
const error = new Error('test')
const serialized = serializeError(error)
expect(Object.getPrototypeOf(serialized)).toBe(null)
expect(serialized).toEqual({
constructor: 'Function<Error>',
name: 'Error',
message: 'test',
stack: expect.any(String),
toString: 'Function<toString>',
})
})
it('Should not fail on errored getters/setters', () => {
const error = new Error('test')
Object.defineProperty(error, 'unserializable', {
get() {
throw new Error('I am unserializable')
},
set() {
throw new Error('I am unserializable')
},
})
Object.defineProperty(error, 'array', {
value: [{
get name() {
throw new Error('name cannnot be accessed')
},
}],
})
expect(serializeError(error)).toEqual({
array: [
{
name: '<unserializable>: name cannnot be accessed',
},
],
constructor: 'Function<Error>',
message: 'test',
name: 'Error',
stack: expect.stringContaining('Error: test'),
toString: 'Function<toString>',
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'),
})
})
it('correctly serialized immutables', () => {
const immutableList = {
'@@__IMMUTABLE_ITERABLE__@@': true,
toJSON() {
return ['foo']
},
}
const immutableRecord = {
'@@__IMMUTABLE_RECORD__@@': true,
toJSON() {
return { foo: 'bar' }
},
}
const error = new Error('test')
Object.assign(error, {
immutableList,
immutableRecord,
})
expect(serializeError(error)).toMatchObject({
stack: expect.stringContaining('Error: test'),
immutableList: ['foo'],
immutableRecord: { foo: 'bar' },
name: 'Error',
message: 'test',
})
})
})