Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: jest could detect some errors it is currently silencing when using jsdom environment #14984

Closed
yjaaidi opened this issue Mar 21, 2024 · 3 comments

Comments

@yjaaidi
Copy link

yjaaidi commented Mar 21, 2024

馃殌 Feature Proposal

The following tests will not show any error when using jsdom environment:

test('leak microtask', () => {
  setImmediate(() => expect(1).toBe(2));
});

test('leak timer', () => {
  setTimeout(() => expect(1).toBe(2));
});

It would be nice if Jest could detect pending timers or wait for them to finish on environment teardown.

There are different options. The cheapest workaround is the one used in vitest but it will only flush queued macrotasks and 0s timers:
https://github.com/vitest-dev/vitest/blob/fee7d8be9d6e6f710270600ae91fa35d861b7075/packages/vitest/src/runtime/setup-node.ts#L85

Ideally, in addition to this, it would be nice to grab the pending timers from jsdom and show a warning or an error depending on some option like detectPendingTimers or simply wait until they all complete (or timeout).
https://github.com/jsdom/jsdom/blob/2f8a7302a43fff92f244d5f3426367a8eb2b8896/lib/jsdom/browser/Window.js#L494
(happy-dom exposes a waitUntilComplete() function, we could contribute to jsdom in order to expose something similar)

Credits go to @rainerhahnekamp for raising the issue.

I'll be happy to help with a PR.

Motivation

This will help Jest detect some issues that are currently silenced. This aligns with how vitest jsdom env works.

Example

No response

Pitch

This will help Jest detect some issues that are currently silenced.

Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Apr 20, 2024
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

1 similar comment
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant