-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
snapshot.test.ts
84 lines (70 loc) · 1.8 KB
/
snapshot.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
import { expect, test } from 'vitest'
import { testOutsideInlineSnapshot } from './snapshots-outside'
test('object', () => {
expect({
this: { is: new Set(['of', 'snapshot']) },
}).toMatchSnapshot()
})
test('multiline', () => {
expect(`
Hello
World
`).toMatchSnapshot()
})
test('from outside', () => {
testOutsideInlineSnapshot()
})
test('with big array', () => {
expect({
this: { is: new Set(['one', new Array(30).fill({})]) },
}).toMatchSnapshot()
})
test('with big string', () => {
expect({
this: { is: new Set(['one', new Array(30).fill('zoo').join()]) },
}).toMatchSnapshot()
})
test('throwing', async () => {
expect(() => {
throw new Error('omega')
}).toThrowErrorMatchingSnapshot()
expect(() => {
// eslint-disable-next-line no-throw-literal
throw 'omega'
}).toThrowErrorMatchingSnapshot()
expect(() => {
// eslint-disable-next-line no-throw-literal
throw { error: 'omega' }
}).toThrowErrorMatchingSnapshot()
await expect(async () => {
throw new Error('omega')
}).rejects.toThrowErrorMatchingSnapshot()
})
test('throwing expect should be a function', async () => {
expect(() => {
expect(new Error('omega')).toThrowErrorMatchingSnapshot()
}).toThrow(/expected must be a function/)
})
test('properties snapshot', () => {
const user = {
createdAt: new Date(),
id: Math.floor(Math.random() * 20),
name: 'LeBron James',
}
expect(user).toMatchSnapshot({
createdAt: expect.any(Date),
id: expect.any(Number),
name: expect.stringContaining('LeBron'),
})
})
test.fails('properties snapshot fails', () => {
const user = {
createdAt: new Date(),
id: Math.floor(Math.random() * 20),
name: 'LeBron James',
}
expect(user).toMatchSnapshot({
createdAt: expect.any(Date),
id: expect.any(String),
})
})