Skip to content

Commit 55ff2f2

Browse files
adapt the waitFor resolve test to make it more readable
1 parent 6b41483 commit 55ff2f2

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

src/__tests__/wait-for.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,11 @@ test('the real timers => fake timers error shows the original stack trace when c
292292

293293
test('does not work after it resolves', async () => {
294294
jest.useFakeTimers('modern')
295-
let context = 'initial'
295+
const contextStack = []
296296
configure({
297297
// @testing-library/react usage to ensure `IS_REACT_ACT_ENVIRONMENT` is set when acting.
298298
unstable_advanceTimersWrapper: callback => {
299-
const originalContext = context
300-
context = 'act'
299+
contextStack.push('act:start')
301300
try {
302301
const result = callback()
303302
// eslint-disable-next-line jest/no-if, jest/no-conditional-in-test -- false-positive
@@ -307,32 +306,31 @@ test('does not work after it resolves', async () => {
307306
then: (resolve, reject) => {
308307
thenable.then(
309308
returnValue => {
310-
context = originalContext
309+
contextStack.push('act:end')
311310
resolve(returnValue)
312311
},
313312
error => {
314-
context = originalContext
313+
contextStack.push('act:end')
315314
reject(error)
316315
},
317316
)
318317
},
319318
}
320319
} else {
321-
context = originalContext
320+
contextStack.push('act:end')
322321
return undefined
323322
}
324323
} catch {
325-
context = originalContext
324+
contextStack.push('act:end')
326325
return undefined
327326
}
328327
},
329328
asyncWrapper: async callback => {
330-
const originalContext = context
331-
context = 'no-act'
329+
contextStack.push('no-act:start')
332330
try {
333331
await callback()
334332
} finally {
335-
context = originalContext
333+
contextStack.push('no-act:end')
336334
}
337335
},
338336
})
@@ -342,6 +340,7 @@ test('does not work after it resolves', async () => {
342340
data = 'resolved'
343341
}, 100)
344342

343+
contextStack.push('waitFor:start')
345344
await waitFor(
346345
() => {
347346
// eslint-disable-next-line jest/no-conditional-in-test -- false-positive
@@ -351,10 +350,29 @@ test('does not work after it resolves', async () => {
351350
},
352351
{interval: 50},
353352
)
354-
355-
expect(context).toEqual('initial')
353+
contextStack.push('waitFor:end')
354+
355+
expect(contextStack).toEqual([
356+
'waitFor:start',
357+
'no-act:start',
358+
'act:start',
359+
'act:end',
360+
'act:start',
361+
'act:end',
362+
'no-act:end',
363+
'waitFor:end',
364+
])
356365

357366
await Promise.resolve()
358367

359-
expect(context).toEqual('initial')
368+
expect(contextStack).toEqual([
369+
'waitFor:start',
370+
'no-act:start',
371+
'act:start',
372+
'act:end',
373+
'act:start',
374+
'act:end',
375+
'no-act:end',
376+
'waitFor:end',
377+
])
360378
})

0 commit comments

Comments
 (0)