forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.spec.ts
98 lines (85 loc) · 2.37 KB
/
utils.spec.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
import { describe, expect, test } from 'vitest'
import { assertTypes, deepMerge } from '../../../packages/vitest/src/utils'
import { deepMergeSnapshot } from '../../../packages/vitest/src/integrations/snapshot/port/utils'
describe('assertTypes', () => {
test('the type of value should be number', () => {
const value = 5
const value_string = '5'
assertTypes(value, 'value', ['number'])
expect(() => assertTypes(value_string, 'value_string', ['number'])).toThrow()
})
test('the type of value should be number or BigInt', () => {
const value_number = 5
const value_bigint = BigInt(5)
const value_string = '5'
assertTypes(value_number, 'value_number', ['number', 'bigint'])
assertTypes(value_bigint, 'value_bigint', ['number', 'bigint'])
expect(() => assertTypes(value_string, 'value_string', ['number', 'bigint'])).toThrow()
})
})
describe('deepMerge', () => {
test('non plain objects retain their prototype, arrays are not merging, plain objects are merging', () => {
class Test {
baz = 'baz'
get foo() {
return 'foo'
}
}
const testA = new Test()
const testB = new Test()
const a = {
test: testA,
num: 30,
array: [1, 2],
obj: {
foo: 'foo',
},
}
const b = {
test: testB,
num: 40,
array: [3, 4],
obj: {
baz: 'baz',
},
}
const merged = deepMerge(a, b)
expect(merged.test instanceof Test).toBe(true)
expect(merged.num).toBe(40)
expect(merged.array).toEqual([3, 4])
expect(merged.obj).toEqual({
foo: 'foo',
baz: 'baz',
})
})
test('deepMergeSnapshot considers asymmetric matcher', () => {
class Test {
zoo = 'zoo'
get bar() {
return 'name'
}
}
const obj = deepMergeSnapshot({
regexp: /test/,
test: new Test(),
name: 'name',
foo: 5,
array: [/test/, 'test'],
}, {
name: expect.stringContaining('name'),
foo: 88,
array: [/test2/],
test: { baz: 'baz' },
})
expect(obj.regexp instanceof RegExp).toBe(true)
expect(obj.test instanceof Test).toBe(false)
expect(obj.array[0] instanceof RegExp).toBe(false)
expect(obj).toEqual({
regexp: /test/,
test: { baz: 'baz', zoo: 'zoo' },
name: expect.stringContaining('name'),
foo: 88,
array: [{}, 'test'],
})
})
})