Using delay('infinite') leaves unresolved promises which make jest warn
See original GitHub issueDescribe the bug
⚠️ I’m not sure if this is the correct repo to report this.
When trying to use delay('infinite')
to mock loading states, jest throws a warning and hangs forever.
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped
in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot
this issue.
This happens because the promises hang forever, but I can’t find a way to cancel/reject/terminate them. I’m not really sure what’s the correct way of handling this.
Environment
msw: 0.29.0
nodejs: 14.17.0
npm: 7.15.1
To Reproduce
// sample.spec.js
const { rest } = require('msw');
const { setupServer } = require('msw/node');
const fetch = require('node-fetch');
it('hangs', () => {
const server = setupServer();
server.listen();
server.use(rest.get('http://localhost/a-request', (_req, res, ctx) => res(ctx.delay('infinite'))));
fetch('http://localhost/a-request').then(() => undefined).catch(console.error);
expect(true).toBe(true);
server.close();
});
Just to give some extra context, the real code is more like:
it('shows a loading spinner', async () => {
render(<Component />);
const loadingBar = await findByRole('progressbar')
expect(loadingBar).toBeInTheDocument();
});
Expected behavior
Requests are aborted(?), the tests conclude and jest doesn’t warn about opened handlers.
Screenshots
Issue Analytics
- State:
- Created 2 years ago
- Reactions:10
- Comments:24 (10 by maintainers)
Top Results From Across the Web
Jest unresolved promise do not fail - Stack Overflow
Ok, I've understand the problem. The cause is the mono thread nature of js. In the two examples of the edit section, te...
Read more >Testing Asynchronous Code - Jest
Promises. Return a promise from your test, and Jest will wait for that promise to resolve. If the promise is rejected, the test...
Read more >Infinite Jest: A Novel › Customer reviews - Amazon.com
Find helpful customer reviews and review ratings for Infinite Jest: A Novel at Amazon.com. Read honest and unbiased product reviews from our users....
Read more >Documentation - SolidJS · Reactive Javascript Library
This approach makes reactivity composable: calling one function within another function ... Thus you should use all dependencies before the promise.
Read more >Hamlet - AP English IV: Literature and Composition
Soon after, Hamlet delays killing Claudius because the King is at prayer, ... Polonius promises Claudius that he will return with a full...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I just ran into this issue as well. It’d be great to have support for this as currently quite a lot of my tests have to use an arbitrary delay which makes them flaky.
I think with the new interceptors architecture we should be able to hard-cancel all pending promises on
process.on('exit')
and friends. It’s a matter of force-resolving/rejecting this Promise queue. At least, this is a good place to start, as all interceptors facilitateAsyncEventEmitter
to await potential listeners.Contributions are welcome!