-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
test-extend.test.ts
332 lines (276 loc) · 9.06 KB
/
test-extend.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
/* eslint-disable prefer-rest-params */
/* eslint-disable no-empty-pattern */
import type { InferFixturesTypes } from '@vitest/runner'
import type { TestAPI } from 'vitest'
import { afterAll, afterEach, beforeEach, describe, expect, expectTypeOf, test, vi } from 'vitest'
interface Fixtures {
todoList: number[]
doneList: number[]
archiveList: number[]
}
const todoList: number[] = [1, 2, 3]
const doneList: number[] = []
const archiveList: number[] = []
const todoFn = vi.fn()
const doneFn = vi.fn()
const myTest = test
.extend<Pick<Fixtures, 'todoList'>>({
todoList: async ({}, use) => {
todoFn()
await use(todoList)
// cleanup
todoFn.mockClear()
todoList.length = 0
todoList.push(1, 2, 3)
},
})
.extend<Pick<Fixtures, 'doneList' | 'archiveList'>>({
doneList: async ({}, use) => {
doneFn()
await use(doneList)
// cleanup
doneFn.mockClear()
doneList.length = 0
},
archiveList,
})
describe('test.extend()', () => {
test('types', () => {
interface TypesContext {
number: number
array: number[]
string: string
any: any
boolean: boolean
func: (a: number, b: string) => void
}
const typesTest = test.extend<TypesContext>({
number: 1,
array: [1, 2, 3],
async string({ }, use) {
await use('string')
},
async any({}, use) {
await use({})
},
boolean: true,
func: async ({}, use): Promise<void> => {
await use(() => undefined)
},
})
expectTypeOf(typesTest).toEqualTypeOf<TestAPI<InferFixturesTypes<typeof typesTest>>>()
})
describe('basic', () => {
myTest('todoList and doneList', ({ todoList, doneList, archiveList }) => {
expect(todoFn).toBeCalledTimes(1)
expect(doneFn).toBeCalledTimes(1)
expectTypeOf(todoList).toEqualTypeOf<number[]>()
expectTypeOf(doneList).toEqualTypeOf<number[]>()
expectTypeOf(doneList).toEqualTypeOf<number[]>()
expect(todoList).toEqual([1, 2, 3])
expect(doneList).toEqual([])
expect(archiveList).toEqual([])
doneList.push(todoList.shift()!)
expect(todoList).toEqual([2, 3])
expect(doneList).toEqual([1])
doneList.push(todoList.shift()!)
expect(todoList).toEqual([3])
expect(doneList).toEqual([1, 2])
archiveList.push(todoList.shift()!)
expect(todoList).toEqual([])
expect(archiveList).toEqual([3])
archiveList.pop()
})
})
describe('smartly init fixtures', () => {
myTest('should not init any fixtures', function () {
expect(todoFn).not.toBeCalled()
expect(doneFn).not.toBeCalled()
expectTypeOf(arguments[0].todoList).not.toEqualTypeOf<number[]>()
expectTypeOf(arguments[0].doneList).not.toEqualTypeOf<number[]>()
expectTypeOf(arguments[0].archiveList).not.toEqualTypeOf<number[]>()
expect(arguments[0].todoList).toBeUndefined()
expect(arguments[0].doneList).toBeUndefined()
expect(arguments[0].archiveList).toBeUndefined()
})
myTest('should not init any fixtures', function ({}) {
expect(todoFn).not.toBeCalled()
expect(doneFn).not.toBeCalled()
expectTypeOf(arguments[0].todoList).not.toEqualTypeOf<number[]>()
expectTypeOf(arguments[0].doneList).not.toEqualTypeOf<number[]>()
expectTypeOf(arguments[0].archiveList).not.toEqualTypeOf<number[]>()
expect(arguments[0].todoList).toBeUndefined()
expect(arguments[0].doneList).toBeUndefined()
expect(arguments[0].archiveList).toBeUndefined()
})
myTest('should only init todoList', function ({ todoList }) {
expect(todoFn).toBeCalledTimes(1)
expect(doneFn).not.toBeCalled()
expectTypeOf(todoList).toEqualTypeOf<number[]>()
expectTypeOf(arguments[0].doneList).not.toEqualTypeOf<number[]>()
expectTypeOf(arguments[0].archiveList).not.toEqualTypeOf<number[]>()
expect(arguments[0].doneList).toBeUndefined()
expect(arguments[0].archiveList).toBeUndefined()
})
myTest('should only init todoList and doneList', function ({ todoList, doneList }) {
expect(todoFn).toBeCalledTimes(1)
expect(doneFn).toBeCalledTimes(1)
expectTypeOf(todoList).toEqualTypeOf<number[]>()
expectTypeOf(doneList).toEqualTypeOf<number[]>()
expectTypeOf(arguments[0].archiveList).not.toEqualTypeOf<number[]>()
expect(todoList).toEqual([1, 2, 3])
expect(doneList).toEqual([])
expect(arguments[0].archiveList).toBeUndefined()
})
myTest('should only init doneList and archiveList', function ({ doneList, archiveList }) {
expect(doneFn).toBeCalledTimes(1)
expectTypeOf(doneList).toEqualTypeOf<number[]>()
expectTypeOf(archiveList).toEqualTypeOf<number[]>()
expectTypeOf(arguments[0].todoList).not.toEqualTypeOf<number[]>()
expect(doneList).toEqual([])
expect(archiveList).toEqual([])
expect(arguments[0].todoList).toBeUndefined()
})
})
describe('test function', () => {
myTest('prop alias', ({ todoList: todos, doneList: done, archiveList: archive }) => {
expect(todoFn).toBeCalledTimes(1)
expect(doneFn).toBeCalledTimes(1)
expectTypeOf(todos).toEqualTypeOf<number[]>()
expectTypeOf(done).toEqualTypeOf<number[]>()
expectTypeOf(archive).toEqualTypeOf<number[]>()
expect(todos).toEqual([1, 2, 3])
expect(done).toEqual([])
expect(archive).toEqual([])
})
})
describe('fixture call times', () => {
const apiFn = vi.fn(() => true)
const serviceFn = vi.fn(() => true)
const teardownFn = vi.fn()
interface APIFixture {
api: boolean
service: boolean
}
const testAPI = test.extend<APIFixture>({
api: async ({}, use) => {
await use(apiFn())
apiFn.mockClear()
teardownFn()
},
service: async ({}, use) => {
await use(serviceFn())
serviceFn.mockClear()
teardownFn()
},
})
beforeEach<APIFixture>(({ api, service }) => {
expect(api).toBe(true)
expect(service).toBe(true)
})
testAPI('Should init 1 time', ({ api }) => {
expect(api).toBe(true)
expect(apiFn).toBeCalledTimes(1)
})
testAPI('Should init 1 time has multiple fixture', ({ api, service }) => {
expect(api).toBe(true)
expect(service).toBe(true)
expect(serviceFn).toBeCalledTimes(1)
expect(apiFn).toBeCalledTimes(1)
})
afterEach<APIFixture>(({ api, service }) => {
expect(api).toBe(true)
expect(service).toBe(true)
expect(apiFn).toBeCalledTimes(1)
expect(serviceFn).toBeCalledTimes(1)
})
afterAll(() => {
expect(serviceFn).toBeCalledTimes(0)
expect(apiFn).toBeCalledTimes(0)
expect(teardownFn).toBeCalledTimes(4)
})
})
describe('fixture in nested describe', () => {
interface Fixture {
foo: number
bar: number
}
const fooFn = vi.fn(() => 0)
const fooCleanup = vi.fn()
const barFn = vi.fn(() => 0)
const barCleanup = vi.fn()
const nestedTest = test.extend<Fixture>({
async foo({}, use) {
await use(fooFn())
fooCleanup()
},
async bar({}, use) {
await use(barFn())
barCleanup()
},
})
beforeEach<Fixture>(({ foo }) => {
expect(foo).toBe(0)
})
nestedTest('should only initialize foo', ({ foo }) => {
expect(foo).toBe(0)
expect(fooFn).toBeCalledTimes(1)
expect(barFn).toBeCalledTimes(0)
})
describe('level 2, using both foo and bar together', () => {
beforeEach<Fixture>(({ foo, bar }) => {
expect(foo).toBe(0)
expect(bar).toBe(0)
})
nestedTest('should initialize foo and bar', ({ foo, bar }) => {
expect(foo).toBe(0)
expect(bar).toBe(0)
expect(fooFn).toBeCalledTimes(2)
expect(barFn).toBeCalledTimes(1)
})
afterEach<Fixture>(({ foo, bar }) => {
expect(foo).toBe(0)
expect(bar).toBe(0)
})
afterAll(() => {
expect(barFn).toHaveBeenCalledTimes(1)
expect(barCleanup).toHaveBeenCalledTimes(1)
expect(fooFn).toHaveBeenCalledTimes(2)
expect(barCleanup).toHaveBeenCalledTimes(1)
})
})
nestedTest('should initialize foo again', ({ foo }) => {
expect(foo).toBe(0)
expect(fooFn).toBeCalledTimes(3)
})
afterEach<Fixture>(({ foo }) => {
expect(foo).toBe(0)
})
afterAll(() => {
expect(fooFn).toHaveBeenCalledTimes(3)
expect(fooCleanup).toHaveBeenCalledTimes(3)
expect(barFn).toHaveBeenCalledTimes(1)
expect(barCleanup).toHaveBeenCalledTimes(1)
})
})
})
// test extend with top level test
const numbers: number[] = []
const teardownFn = vi.fn()
const teardownTest = test.extend<{
numbers: number[]
}>({
numbers: async ({}, use) => {
numbers.push(1, 2, 3)
await use(numbers)
numbers.splice(0, numbers.length)
teardownFn()
},
})
teardownTest('test without describe', ({ numbers }) => {
expect(numbers).toHaveLength(3)
})
test('teardown should be called once time', () => {
expect(numbers).toHaveLength(0)
expect(teardownFn).toBeCalledTimes(1)
})