{{ message }}
Repository navigation
Expand file tree
/
Copy pathcheck-test-patterns.test.ts
More file actions
387 lines (375 loc) · 11.5 KB
/
Copy pathcheck-test-patterns.test.ts
File metadata and controls
387 lines (375 loc) · 11.5 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import { describe, expect, it } from 'vitest'
import {
findRealWaitViolations,
findStubAndHookViolations,
parseSource,
} from './check-test-patterns'
function rules(source: string, file = 'apps/sim/lib/example.test.ts') {
return findStubAndHookViolations(file, parseSource(file, source)).map(
({ rule, detail }) => `${rule}:${detail}`
)
}
describe('module-scope-stub', () => {
it('flags stubs at module scope, including exported, cast and chained ones', () => {
const source = [
"vi.stubGlobal('fetch', vi.fn())",
"vi.stubEnv('NODE_ENV', 'test')",
"const spy = vi.spyOn(console, 'error')",
"export const fetchMock = vi.stubGlobal('fetch', vi.fn())",
"const cast = vi.spyOn(Date, 'now') as Mock",
"const satisfied = vi.spyOn(Date, 'now') satisfies Mock",
"const chained = vi.spyOn(Date, 'now').mockReturnValue(0)",
"const wrapped = (vi.spyOn(Date, 'now') as Mock).mockReturnValue(0)",
].join('\n')
expect(rules(source)).toEqual([
'module-scope-stub:vi.stubGlobal()',
'module-scope-stub:vi.stubEnv()',
'module-scope-stub:vi.spyOn()',
'module-scope-stub:vi.stubGlobal()',
'module-scope-stub:vi.spyOn()',
'module-scope-stub:vi.spyOn()',
'module-scope-stub:vi.spyOn()',
'module-scope-stub:vi.spyOn()',
])
})
it('ignores stubs inside hooks and tests', () => {
const source = [
'beforeEach(() => {',
" vi.stubGlobal('fetch', vi.fn())",
" vi.spyOn(Date, 'now').mockReturnValue(0)",
'})',
"it('works', () => {",
" vi.stubEnv('NODE_ENV', 'test')",
'})',
].join('\n')
expect(rules(source)).toEqual([])
})
it('exempts integration files', () => {
expect(rules("vi.stubEnv('NODE_ENV', 'test')", 'apps/sim/lib/example.integration.ts')).toEqual(
[]
)
})
})
describe('redundant-hook', () => {
it('flags a leading reset in beforeEach', () => {
const source = ['beforeEach(() => {', ' vi.clearAllMocks()', ' setup()', '})'].join('\n')
expect(rules(source)).toEqual(['redundant-hook:vi.clearAllMocks()'])
})
it('allows a reset after setup statements in beforeEach', () => {
const source = ['beforeEach(() => {', ' setup()', ' vi.clearAllMocks()', '})'].join('\n')
expect(rules(source)).toEqual([])
})
it('flags every reset in afterEach, wherever it sits', () => {
const source = [
'afterEach(() => {',
' cleanup()',
' vi.restoreAllMocks()',
' vi.unstubAllGlobals()',
'})',
].join('\n')
expect(rules(source)).toEqual([
'redundant-hook:vi.restoreAllMocks()',
'redundant-hook:vi.unstubAllGlobals()',
])
})
})
describe('real-wait', () => {
const HELPERS = "import { sleep, interruptibleSleep } from '@sim/utils/helpers'"
function waits(source: string, file = 'apps/sim/lib/example.test.ts') {
return findRealWaitViolations(file, parseSource(file, source)).map(({ detail }) => detail)
}
it.each<[string, string[], string[]]>([
['an imported sleep', [HELPERS, "it('t', async () => { await sleep(10) })"], ['sleep() #1']],
[
'an aliased sleep',
["import { sleep as wait } from '@sim/utils/helpers'", 'await wait(10)'],
['sleep() #1'],
],
[
'interruptibleSleep',
[HELPERS, 'await interruptibleSleep(10, signal)'],
['interruptibleSleep() #1'],
],
['a sleep with a computed delay', [HELPERS, 'await sleep(delayMs)'], ['sleep() #1']],
[
'timers/promises setTimeout',
["import { setTimeout as delay } from 'node:timers/promises'", 'await delay(50)'],
['timers/promises setTimeout() #1'],
],
[
'a promise resolved by setTimeout',
['await new Promise((r) => setTimeout(r, 5))'],
['setTimeout promise #1'],
],
[
'a promise resolved from a setTimeout callback',
['await new Promise<void>((resolve) => { globalThis.setTimeout(() => resolve(), ms) })'],
['setTimeout promise #1'],
],
[
'fake timers that leave setTimeout real',
[HELPERS, "beforeEach(() => { vi.useFakeTimers({ toFake: ['Date'] }) })", 'await sleep(10)'],
['sleep() #1'],
],
[
'fake timers that advance with the wall clock',
[
HELPERS,
"it('t', async () => {",
' vi.useFakeTimers({ shouldAdvanceTime: true })',
' await sleep(10)',
'})',
],
['sleep() #1'],
],
[
'a sleep before the test installs fake timers',
[HELPERS, "it('t', async () => {", ' await sleep(10)', ' vi.useFakeTimers()', '})'],
['sleep() #1'],
],
[
'a sleep in a test beside one that installs fake timers',
[
HELPERS,
"it('a', () => { vi.useFakeTimers() })",
"it('b', async () => { await sleep(10) })",
],
['sleep() #1'],
],
[
'a sleep before the install in the hook that installs fake timers',
[
HELPERS,
"describe('d', () => {",
' beforeEach(async () => {',
' await sleep(100)',
' vi.useFakeTimers()',
' })',
" it('t', async () => { await sleep(10) })",
'})',
],
['sleep() #1'],
],
[
'a sleep after the test restores real timers',
[
HELPERS,
"it('t', async () => {",
' vi.useFakeTimers()',
' vi.useRealTimers()',
' await sleep(10)',
'})',
],
['sleep() #1'],
],
[
'a sleep in a nested suite that restores real timers',
[
HELPERS,
"describe('outer', () => {",
' beforeEach(() => { vi.useFakeTimers() })',
" describe('inner', () => {",
' beforeEach(() => { vi.useRealTimers() })',
" it('t', async () => { await sleep(10) })",
' })',
'})',
],
['sleep() #1'],
],
[
'a sleep in a beforeAll beside a beforeEach that installs fake timers',
[
HELPERS,
'beforeEach(() => { vi.useFakeTimers() })',
'beforeAll(async () => { await sleep(10) })',
],
['sleep() #1'],
],
[
'a sleep in a beforeEach declared before the one that installs fake timers',
[
HELPERS,
'beforeEach(async () => { await sleep(10) })',
'beforeEach(() => { vi.useFakeTimers() })',
],
['sleep() #1'],
],
[
'fake timers with a config the AST cannot read',
[
HELPERS,
'beforeEach(() => { vi.useFakeTimers(config) })',
"it('t', async () => { await sleep(10) })",
],
['sleep() #1'],
],
[
'fake timers with a spread config',
[
HELPERS,
"it('t', async () => {",
' vi.useFakeTimers({ ...config })',
' await sleep(10)',
'})',
],
['sleep() #1'],
],
[
'namespace imports',
[
"import * as helpers from '@sim/utils/helpers'",
"import * as timers from 'node:timers/promises'",
'await helpers.sleep(10)',
'await timers.setTimeout(10)',
],
['sleep() #1', 'timers/promises setTimeout() #1'],
],
[
'a module-scope wait before a module-scope install',
[HELPERS, 'await sleep(10)', 'vi.useFakeTimers()'],
['sleep() #1'],
],
[
'a helper wait after module-scope real timers are restored',
[
HELPERS,
'async function settle() { await sleep(10) }',
'vi.useFakeTimers()',
'vi.useRealTimers()',
],
['sleep() #1'],
],
[
'a helper called at module scope before a module-scope install',
[
HELPERS,
'async function settle() { await sleep(10) }',
'await settle()',
'vi.useFakeTimers()',
],
['sleep() #1'],
],
[
'an arrow helper called at module scope before a module-scope install',
[
HELPERS,
'const settle = async () => { await sleep(10) }',
'await settle()',
'vi.useFakeTimers()',
],
['sleep() #1'],
],
[
'a wrapped arrow helper called at module scope before a module-scope install',
[
HELPERS,
'const settle = (async () => { await sleep(10) }) satisfies (() => Promise<void>)',
'await settle()',
'vi.useFakeTimers()',
],
['sleep() #1'],
],
[
'every occurrence, numbered per kind',
[HELPERS, 'await sleep(10)', 'await new Promise((r) => setTimeout(r, 5))', 'await sleep(20)'],
['sleep() #1', 'setTimeout promise #1', 'sleep() #2'],
],
])('flags %s', (_name, lines, expected) => {
expect(waits(lines.join('\n'))).toEqual(expected)
})
it.each<[string, string[]]>([
[
'a sleep not imported from @sim/utils/helpers',
["import { sleep } from './helpers'", 'await sleep(10)'],
],
[
'a mocked @sim/utils/helpers',
[HELPERS, "vi.mock('@sim/utils/helpers', () => utilsHelpersMock)", 'await sleep(10)'],
],
['a zero-delay sleep', [HELPERS, 'await sleep(0)']],
[
'a zero-delay setTimeout promise',
['await new Promise((r) => setTimeout(r, 0))', 'await new Promise((r) => setTimeout(r))'],
],
[
'a setTimeout that only rejects',
[
"await new Promise((resolve, reject) => setTimeout(() => reject(new Error('timeout')), 50))",
],
],
[
'a sleep after the test installs fake timers',
[
HELPERS,
"it('t', async () => {",
' vi.useFakeTimers()',
' const done = sleep(10)',
' await vi.advanceTimersByTimeAsync(10)',
' await done',
'})',
],
],
[
'a sleep in a describe whose beforeEach installs fake timers',
[
HELPERS,
"describe('d', () => {",
" beforeEach(() => { vi.useFakeTimers({ toFake: ['setTimeout'] }) })",
" it('t', async () => { await sleep(10) })",
'})',
],
],
[
'a sleep under a module-level beforeAll that installs fake timers',
[HELPERS, "it('t', async () => { await sleep(10) })", 'beforeAll(() => vi.useFakeTimers())'],
],
[
'a sleep after the install in the hook that installs fake timers',
[HELPERS, 'beforeAll(async () => {', ' vi.useFakeTimers()', ' await sleep(100)', '})'],
],
[
'a sleep after real timers are restored and fake ones installed again',
[
HELPERS,
"it('t', async () => {",
' vi.useRealTimers()',
' vi.useFakeTimers()',
' await sleep(10)',
'})',
],
],
[
'a sleep in an afterEach beside a beforeEach that installs fake timers',
[
HELPERS,
'beforeEach(() => { vi.useFakeTimers() })',
'afterEach(async () => { await sleep(10) })',
],
],
[
'a helper declared before a module-scope install',
[
HELPERS,
"vi.mock('@/lib/example', () => ({}))",
'const mocks = vi.hoisted(() => ({ run: vi.fn() }))',
'const DELAY_MS = 10',
'type Options = { delay: number }',
'async function settle() { await sleep(DELAY_MS) }',
'const settleLater = async () => { await settle() }',
'const run = vi.fn()',
'const fixture = createFixture()',
'vi.useFakeTimers()',
'await settle()',
"it('t', async () => { await settle() })",
],
],
])('ignores %s', (_name, lines) => {
expect(waits(lines.join('\n'))).toEqual([])
})
it.each(['apps/sim/lib/example.integration.ts', 'apps/sim/tools/example.live.test.ts'])(
'exempts %s, which waits on real services',
(file) => {
expect(waits([HELPERS, 'await sleep(10)'].join('\n'), file)).toEqual([])
}
)
})
You can’t perform that action at this time.
