question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using delay('infinite') leaves unresolved promises which make jest warn

See original GitHub issue

Describe 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

image

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:10
  • Comments:24 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
DannyBiezencommented, Dec 15, 2022

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.

2reactions
kettanaitocommented, Apr 17, 2022

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 facilitate AsyncEventEmitter to await potential listeners.

Contributions are welcome!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found