-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
jsdom console is unmockable #5223
Comments
I've done a little more digging, and I'll open a quick and dirty PR to get some feedback on to address this 👍 |
PR Opened: #5227 |
Could you do this? window.addEventListener('error', e => {
// I want to silence all errors and know what I'm doing
e.preventDefault();
}); It doesn't solve React printing an error as a warning but you could either override |
Hmm... I'll give that a look. That may help with the double-error issue (different but related issue) I'm seeing. Thanks! |
@kentcdodds I detailed this problem at #8393 (comment) |
I fixed this by not playing around with the
Then, in the tests, you can do stuff like |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Do you want to request a feature or report a bug?
Feature. React@16 made some changes that cause this issue.
What is the current behavior?
It's impossible to mock the console used by JSDOM. This is a problem because react-dom uses the browser to dispatch an event:
https://github.com/facebook/react/blob/46b3c3e4ae0d52565f7ed2344036a22016781ca0/packages/shared/invokeGuardedCallback.js#L137-L147
They do this
It's kind of interesting actually. See the comments above the linked code.
But because of this, an error thrown in this way gets logged by JSDOM's VirtualConsole which is initialized in a different environment from my tests. This means it's impossible for me to mock it for an individual test.
That said, I can provide my own stub for JSDOM's
VirtualConsole
, but that will stub it for all tests which could lead to me missing legitimate errors being logged. Also, I can't make any assertions for my stub ofVirtualConsole
.If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can
yarn install
andyarn test
.Here's a repo: https://github.com/kentcdodds/jest-jsdom-react-console-issues
npm/yarn install
npm run test:1
npm run test:2
The
test:2
script uses thejest.custom-config.js
config which provides the stub ofVirtualConsole
and you'll see it's not a problem in that situation, but I have the aforementioned problems.What is the expected behavior?
I would like the
console
used to initialize JSDOM'sVirtualConsole
to be the same one that I have access to in my test so I can mock it.Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.
See the repo above.
Thanks!
The text was updated successfully, but these errors were encountered: