-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.ts
158 lines (138 loc) · 3.94 KB
/
index.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
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
/* eslint-disable n/no-callback-literal */
import axios from 'axios'
import { fetchUser } from './utils/service'
import getGuid, { getYear, getMonth } from './utils/get-info'
// 注意 jest.mock 要在顶层调用,在 test() 调用不生效。
jest.mock('axios')
jest.mock('./utils/get-info', () => {
const originalModule = jest.requireActual('./utils/get-info')
return {
__esModule: true,
...originalModule,
default: () => 'abc',
getYear: () => 2021
}
})
describe('Jest', () => {
describe('断言 API', () => {
test('相等', () => {
expect(7).toBe(7)
expect(7).not.toBe(3)
expect({ name: 'Joel' }).not.toBe({ name: 'Joel' })
})
test('引用类型值的深度相等', () => {
expect({ name: 'Joel' }).toEqual({ name: 'Joel' })
expect([1]).toEqual([1])
expect([1, [[[1]]]]).toEqual([1, [[[1]]]])
})
test('浮动数运算比较', () => {
expect(0.1 + 0.2).not.toBe(0.3)
expect(0.1 + 0.2).toBeCloseTo(0.3)
})
})
describe('回调被执行情况', () => {
test('是否被调用', () => {
const cb = jest.fn()
cb()
expect(cb).toBeCalled()
})
test('被调用次数', () => {
const cb = jest.fn()
cb() // 1
cb() // 2
cb() // 3
cb() // 4
cb() // 5
expect(cb).toHaveBeenCalledTimes(5)
})
test('被调用时的参数', () => {
const cb = jest.fn()
cb(1, 2)
expect(cb).toHaveBeenCalledWith(1, 2)
cb([1], { name: 'Joel' })
expect(cb).toHaveBeenCalledWith([1], { name: 'Joel' })
})
test('被多次调用时的参数', () => {
const cb = jest.fn()
cb('a')
cb('b')
cb('c')
expect(cb).toHaveBeenNthCalledWith(1, 'a')
expect(cb).toHaveBeenNthCalledWith(2, 'b')
expect(cb).toHaveBeenNthCalledWith(3, 'c')
})
})
describe('测试异步代码', () => {
test('回调类型异步: 通过在回调中传入 done', done => {
function fetchNameCallback (cb: (name: string) => void) {
setTimeout(() => {
cb('Joel')
}, 1000)
}
fetchNameCallback(name => {
expect(name).toBe('Joel')
done()
})
})
function fetchName (throwError?: boolean) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (!throwError) {
resolve('Joel')
} else {
// eslint-disable-next-line prefer-promise-reject-errors
reject('error happened')
}
}, 1000)
})
}
test('Promise 类型的异步:正常写', () => {
fetchName().then(name => {
expect(name).toBe('Joel')
})
fetchName(true).catch(e => {
expect(e).toMatch('error')
})
})
test('Async/Await 类型的异步:正常写', async () => {
const name = await fetchName()
expect(name).toBe('Joel')
try {
await fetchName(true)
} catch (e) {
expect(e).toMatch('error')
}
})
})
describe('测试异常', () => {
test('测试异常', () => {
function throwErrorFn () {
throw new Error('Error happened')
}
expect(throwErrorFn).toThrow()
expect(throwErrorFn).toThrow('Error happened')
expect(throwErrorFn).toThrow(/happened/)
expect(throwErrorFn).toThrow(Error('Error happened'))
})
})
describe('Mock', () => {
test('Mock 第三方包: jest.mock("axios")', () => {
const _axiosPrev = axios.get;
(axios.get as any).mockImplementation((url: string) => {
if (/^\/user$/.test(url)) {
return Promise.resolve({ name: 'Joel' })
}
return Promise.resolve('other')
})
fetchUser().then(({ name }) => {
expect(name).toBe('Joel')
axios.get = _axiosPrev
})
})
test('Mock 文件的全部或部分内容', () => {
expect(getGuid()).toBe('abc')
expect(getYear()).toBe(2021)
expect(getMonth()).toBe(9)
})
})
})