/
jest-mock.test.ts
335 lines (247 loc) · 7.39 KB
/
jest-mock.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { describe, expect, it, vi } from 'vitest'
describe('jest mock compat layer', () => {
const returnFactory = (type: string) => (value: any) => ({ type, value })
const r = returnFactory('return')
const e = returnFactory('throw')
it('works with name', () => {
const spy = vi.fn()
spy.mockName('spy test name')
expect(spy.getMockName()).toBe('spy test name')
})
it('clearing', () => {
const spy = vi.fn()
spy('hello')
expect(spy.mock.calls).toHaveLength(1)
expect(spy.mock.calls[0]).toEqual(['hello'])
spy('world')
expect(spy.mock.calls).toEqual([['hello'], ['world']])
spy.mockReset() // same as mockClear()
expect(spy.mock.calls).toEqual([])
})
it('clearing instances', () => {
const Spy = vi.fn(() => ({}))
expect(Spy.mock.instances).toHaveLength(0)
// eslint-disable-next-line no-new
new Spy()
expect(Spy.mock.instances).toHaveLength(1)
Spy.mockReset() // same as mockClear()
expect(Spy.mock.instances).toHaveLength(0)
})
it('implementation is set correctly on init', () => {
const impl = () => 1
const mock1 = vi.fn(impl)
expect(mock1.getMockImplementation()).toEqual(impl)
const mock2 = vi.fn()
expect(mock2.getMockImplementation()).toBeUndefined()
})
it('implementation sync fn', () => {
const originalFn = function () {
return 'original'
}
const spy = vi.fn(originalFn)
spy() // returns 'original'
expect(spy.getMockImplementation()).toBe(originalFn)
spy.mockReturnValueOnce('2-once').mockReturnValueOnce('3-once')
spy() // returns '2-once'
spy() // returns '3-once'
spy() // returns 'original'
const implOnce = () => 'once'
spy.mockImplementationOnce(implOnce)
spy() // returns 'once'
spy() // returns 'original'
expect(spy.getMockImplementation() === implOnce).toBe(false) // jest doesn't store Once implementations
const impl = () => 'unlimited'
spy.mockImplementation(impl)
spy() // returns 'unlimited'
spy()
spy()
expect(spy.getMockImplementation() === impl).toBe(true)
spy.mockReturnValue('return-unlimited')
spy()
spy()
expect(spy.mock.results).toEqual([
r('original'),
r('2-once'),
r('3-once'),
r('original'),
r('once'),
r('original'),
r('unlimited'),
r('unlimited'),
r('unlimited'),
r('return-unlimited'),
r('return-unlimited'),
])
spy.mockRestore()
expect(spy.getMockImplementation()).toBe(undefined)
expect(spy.mock.results).toEqual([])
})
it('implementation async fn', async () => {
const originalFn = async function () {
return 'original'
}
const spy = vi.fn(originalFn)
await spy() // returns 'original'
spy
.mockResolvedValue('unlimited')
.mockResolvedValueOnce('3-once')
.mockResolvedValueOnce('4-once')
await spy()
await spy()
await spy()
await spy()
expect(spy.mock.results).toEqual([
r('original'),
r('3-once'),
r('4-once'),
r('unlimited'),
r('unlimited'),
])
})
it('invocationOrder', () => {
const a = vi.fn()
const b = vi.fn()
a()
b()
expect(a.mock.invocationCallOrder[0]).toBeLessThan(b.mock.invocationCallOrder[0])
})
it('getter spyOn', () => {
const obj = {
get getter() {
return 'original'
},
}
const spy = vi.spyOn(obj, 'getter', 'get')
expect(obj.getter).toBe('original')
spy.mockImplementation(() => 'mocked').mockImplementationOnce(() => 'once')
expect(obj.getter).toBe('once')
expect(obj.getter).toBe('mocked')
expect(obj.getter).toBe('mocked')
spy.mockReturnValue('returned').mockReturnValueOnce('returned-once')
expect(obj.getter).toBe('returned-once')
expect(obj.getter).toBe('returned')
expect(obj.getter).toBe('returned')
spy.mockRestore()
expect(obj.getter).toBe('original')
})
it('getter function spyOn', () => {
const obj = {
get getter() {
return function () {
return 'original'
}
},
}
const spy = vi.spyOn(obj, 'getter')
expect(obj.getter()).toBe('original')
spy.mockImplementation(() => 'mocked').mockImplementationOnce(() => 'once')
expect(obj.getter()).toBe('once')
expect(obj.getter()).toBe('mocked')
expect(obj.getter()).toBe('mocked')
})
it('setter spyOn', () => {
let setValue = 'original'
let mockedValue = 'none'
const obj = {
get setter() {
return setValue
},
set setter(v: any) {
setValue = v
},
}
const spy = vi.spyOn(obj, 'setter', 'set')
obj.setter = 'first'
expect(setValue).toBe('first')
expect(mockedValue).toBe('none')
spy.mockImplementation(() => (mockedValue = 'mocked')).mockImplementationOnce(() => (mockedValue = 'once'))
obj.setter = 'i can do whatever'
expect(mockedValue).toBe('once')
expect(setValue).toBe('first')
obj.setter = 'does nothing'
expect(mockedValue).toBe('mocked')
expect(setValue).toBe('first')
obj.setter = 'since setter is mocked'
expect(mockedValue).toBe('mocked')
expect(setValue).toBe('first')
spy.mockRestore()
obj.setter = 'last'
expect(spy.getMockImplementation()).toBe(undefined)
expect(setValue).toBe('last')
})
it('should work - setter', () => {
const obj = {
_property: false,
set property(value) {
this._property = value
},
get property() {
return this._property
},
}
const spy = vi.spyOn(obj, 'property', 'set')
obj.property = true
expect(spy).toHaveBeenCalled()
expect(obj.property).toBe(true)
obj.property = false
spy.mockRestore()
obj.property = true
// unlike jest, mockRestore only restores implementation to the original one,
// we are still spying on the setter
expect(spy).toHaveBeenCalled()
expect(obj.property).toBe(true)
})
it('throwing', async () => {
const fn = vi.fn(() => {
// eslint-disable-next-line no-throw-literal
throw 'error'
})
try {
fn()
}
catch {}
expect(fn.mock.results).toEqual([
e('error'),
])
})
it('mockRejectedValue', async () => {
const safeCall = async (fn: () => void) => {
try {
await fn()
}
catch {}
}
const spy = vi.fn()
.mockRejectedValue(new Error('error'))
.mockRejectedValueOnce(new Error('once'))
await safeCall(spy)
await safeCall(spy)
expect(spy.mock.results[0]).toEqual(e(new Error('once')))
expect(spy.mock.results[1]).toEqual(e(new Error('error')))
})
it('mockResolvedValue', async () => {
const spy = vi.fn()
.mockResolvedValue('resolved')
.mockResolvedValueOnce('once')
await spy()
await spy()
expect(spy.mock.results[0]).toEqual(r('once'))
expect(spy.mock.results[1]).toEqual(r('resolved'))
})
it('tracks instances made by mocks', () => {
const Fn = vi.fn()
expect(Fn.mock.instances).toEqual([])
const instance1 = new Fn()
expect(Fn.mock.instances[0]).toBe(instance1)
const instance2 = new Fn()
expect(Fn.mock.instances[1]).toBe(instance2)
})
it('.mockRestore() should restore initial implementation', () => {
const testFn = vi.fn(() => true)
expect(testFn()).toBe(true)
testFn.mockReturnValue(false)
expect(testFn()).toBe(false)
testFn.mockRestore()
expect(testFn()).toBe(true)
})
})