react-testing-library: auto cleanup will break down testing in `mocha` & `esm` environment
@testing-library/reactversion:9.5.0reactversion:^16.12.0nodeversion:12.13.0mochaversion:^6.2.2,mochawesomeversion:^4.1.0,esmversion:^3.2.25npm(oryarn) version:yarn@1.17.3
Relevant code or config:
Github demo as below:
https://github.com/whodoeshua/mocha-react-testing-library
package.json
{
"scripts": {
"test": "tsc --pretty && mocha --exit --trace-warnings -r source-map-support/register -r jsdom-global/register -r esm -c \"build/**/@(skip|test)-*.js\""
},
}
What you did:
yarn test
What happened:
Global afterEach hook will beak down testing with timeout error, cause of
1) "after each" hook for "test":
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7)
Reproduction:
https://github.com/whodoeshua/mocha-react-testing-library
Problem description:
This problem is cause of auto cleanup. Auto clean up is an async funtion and required timers module to make a fake timer. But require function will throw out a error once esm module has been required. And the async cleanup will never stop.
Auto cleanup will call flush-microtasks.
// @testing-library/react/src/index.js:9
if (typeof afterEach === 'function' && !process.env.RTL_SKIP_AUTO_CLEANUP) {
afterEach(async () => {
await flush()
cleanup()
})
}
require function in flush-microtasks will throw out an error.
// @testing-library/react/src/flush-microtasks.js:18
enqueueTask = nodeRequire('timers').setImmediate
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 3
- Comments: 17 (13 by maintainers)
Commits related to this issue
- fix: restore this when requiring timers (#644) closes #614 — committed to testing-library/react-testing-library by just-boris 4 years ago
yeah this looks good. I’d recommend sending a PR to react as well.
The failure case is likely the only one that isn’t transpiled by webpack and uses the actual
require()from node not the bundled webpack_require.require()throws if used in a file using esmodules.I’m pretty sure this hack was used to prevent rollup from adding a node polyfill but I don’t know why/if you couldn’t explicitly tell rollup to not do it. Either way @threepointone hopefully has more context.
I think what I want is not remove async cleanup in testing but make sure
nodeRequiredcould run in esm environment. Otherwise I think throw out an error to tell developer what is happening is better than catch it and just waiting for timeout. That will be a problem once I set up a long timeout value.