Skip to content

Commit ce11c0f

Browse files
adding reproductible test
1 parent 86c5c08 commit ce11c0f

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

src/__tests__/wait-for.js

+64-3
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,18 @@ test('does not work after it resolves', async () => {
335335
},
336336
})
337337

338-
let data = null
338+
let timeoutResolved = false
339339
setTimeout(() => {
340340
contextStack.push('timeout')
341-
data = 'resolved'
341+
timeoutResolved = true
342342
}, 100)
343343

344344
contextStack.push('waitFor:start')
345345
await waitFor(
346346
() => {
347347
contextStack.push('callback')
348348
// eslint-disable-next-line jest/no-conditional-in-test -- false-positive
349-
if (data === null) {
349+
if (!timeoutResolved) {
350350
throw new Error('not found')
351351
}
352352
},
@@ -391,3 +391,64 @@ test('does not work after it resolves', async () => {
391391
]
392392
`)
393393
})
394+
395+
test(`when fake timer is installed, on waitFor timeout, it doesn't call the callback afterward`, async () => {
396+
jest.useFakeTimers('modern')
397+
398+
configure({
399+
// @testing-library/react usage to ensure `IS_REACT_ACT_ENVIRONMENT` is set when acting.
400+
unstable_advanceTimersWrapper: callback => {
401+
try {
402+
const result = callback()
403+
// eslint-disable-next-line jest/no-if, jest/no-conditional-in-test -- false-positive
404+
if (typeof result?.then === 'function') {
405+
const thenable = result
406+
return {
407+
then: (resolve, reject) => {
408+
thenable.then(
409+
returnValue => {
410+
resolve(returnValue)
411+
},
412+
error => {
413+
reject(error)
414+
},
415+
)
416+
},
417+
}
418+
} else {
419+
return undefined
420+
}
421+
} catch {
422+
return undefined
423+
}
424+
},
425+
asyncWrapper: async callback => {
426+
try {
427+
await callback()
428+
} finally {
429+
/* eslint no-empty: "off" */
430+
}
431+
},
432+
})
433+
434+
let waitForHasResolved = false
435+
let callbackCalledAfterWaitForResolved = false
436+
437+
await expect(() =>
438+
waitFor(
439+
() => {
440+
// eslint-disable-next-line jest/no-conditional-in-test -- false-positive
441+
if (waitForHasResolved) {
442+
callbackCalledAfterWaitForResolved = true
443+
}
444+
throw new Error('We want to timeout')
445+
},
446+
{interval: 50, timeout: 100},
447+
),
448+
).rejects.toThrow()
449+
waitForHasResolved = true
450+
451+
await Promise.resolve()
452+
453+
expect(callbackCalledAfterWaitForResolved).toBe(false)
454+
})

0 commit comments

Comments
 (0)