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:
They do this
To preserve the expected "Pause on exceptions" behavior
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 of VirtualConsole.
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 and yarn test.
Here's a repo: https://github.com/kentcdodds/jest-jsdom-react-console-issues
npm/yarn installnpm run test:1npm run test:2The test:2 script uses the jest.custom-config.js config which provides the stub of VirtualConsole 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's VirtualConsole 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!
I've done a little more digging, and I'll open a quick and dirty PR to get some feedback on to address this :+1:
PR Opened: https://github.com/facebook/jest/pull/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 console.error and ignore it or help us fix it: https://github.com/facebook/react/issues/11098#issuecomment-355032539
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 https://github.com/facebook/jest/issues/8393#issuecomment-489355303
I fixed this by not playing around with the virtualConsole option because it seems to be inconsistent depending on whether Jest runs in parallel or not, but just this:
beforeEach(() => {
['log', 'warn', 'error'].forEach((type) => {
origConsole[type] = window.console[type]
window.console[type] = jest.fn()
})
})
afterEach(() => {
['log', 'warn', 'error'].forEach((type) => {
window.console[type] = origConsole[type]
})
})
Then, in the tests, you can do stuff like expect(window.console.error).toHaveBeenCalled() and so on.
Most helpful comment
Could you do this?
It doesn't solve React printing an error as a warning but you could either override
console.errorand ignore it or help us fix it: https://github.com/facebook/react/issues/11098#issuecomment-355032539