-
Notifications
You must be signed in to change notification settings - Fork 467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When running in Jest, code assumes setImmediate
and clearImmediate
#914
Comments
If I'm not mistaken, the only place we rely on a global dom-testing-library/src/helpers.js Lines 18 to 25 in 300bfe2
That could probably be changed to use the |
What is it used for? |
So it seems like the place that uses this (linked above) is just this helper which is meant to Therefore, the fix is just to change this code to be resilient to |
|
Proposed fix: const timerAPI = {
clearInterval,
clearTimeout,
setInterval,
setTimeout,
}
if (typeof setImmediate === 'function') {
timerAPI.setImmediate = setImmediate;
}
if (typeof clearImmediate === 'function') {
timerAPI.clearImmediate = clearImmediate;
}
// ... |
This function depends on function waitForDomChange({
container = getDocument(),
timeout = getConfig().asyncUtilTimeout,
mutationObserverOptions = {
subtree: true,
childList: true,
attributes: true,
characterData: true,
},
} = {}) {
if (!hasWarned) {
hasWarned = true
console.warn(
`\`waitForDomChange\` has been deprecated. Use \`waitFor\` instead: https://testing-library.com/docs/dom-testing-library/api-async#waitfor.`,
)
}
return new Promise((resolve, reject) => {
const timer = setTimeout(onTimeout, timeout)
const {MutationObserver} = getWindowFromNode(container)
const observer = new MutationObserver(onMutation)
runWithRealTimers(() =>
observer.observe(container, mutationObserverOptions),
)
function onDone(error, result) {
clearTimeout(timer)
setImmediate(() => observer.disconnect())
if (error) {
reject(error)
} else {
resolve(result)
}
}
function onMutation(mutationsList) {
onDone(null, mutationsList)
}
function onTimeout() {
onDone(new Error('Timed out in waitForDomChange.'), null)
}
})
} |
@renatoalencar is it possible to switch the |
I'm currently trying to see if there's any way to break this, and actually works just fine. Let me do some more tests and see. |
It would be awesome if somebody could verify if this issue is resolved when using "resolutions": {
"**/@testing-library/dom": "https://pkg.csb.dev/testing-library/dom-testing-library/commit/2781ebca/@testing-library/dom"
} to their |
@all-contributors please add @SimenB for bugs |
I've put up a pull request to add @SimenB! 🎉 |
Thanks for all your help here, folks! 🙏 |
@testing-library/dom
version: 7.29.6setImmediate
andclearImmediate
jestjs/jest#11222Relevant code or config:
What you did:
Ran the test without global
setImmediate
andclearImmediate
definedWhat happened:
Reproduction:
See jestjs/jest#11222. Essentially, delete
setImmediate
andclearImmediate
fromglobal
in the JSDOM env Jest sets up.Problem description:
It throws runtime errors at
require
/import
-timeSuggested solution:
Do a
typeof setImmediate !== 'undefined'
before using it.The text was updated successfully, but these errors were encountered: