Jest error: Document is undefined
I recently started using webstorm and now I'm using create.react-app and that gives me a default test file to try App.test.js but when I run the testfile with Jest I get the error that document is undefined.
I can't seem to find any info on how to solve this and the same code is running fine with jest in visual studio code.
Does anyone else have this problem using create-react-app and jest?
(files are completly unaltered so if you wish to see how they look simply run the command and see)
EDIT: Should add that running npm test in the console it passes with no errors so it's only using the quick test launch in webstorm when you "run" the file
I've attached a screenshot of it failing and one where it's working with the console
Please sign in to leave a comment.
Please can you attach a screenshot of the error?
Attached screenshots
Try adding
--env=jsdom
to Jest Options: field
That worked! Strange it didn't in the package.json, but this is much nicer than appending it in the package.
Thanks!
Hi,
I have similar issue and I am not sure how to fix it. Setting Jest Options to --env=jsdom didn't help me. Please suggest.
Test Gets Part Size creates a new instance of object Loader (from Loader.js). As you can notice document is defined.
Loader.js imports three.js. Method loadFiles creates a new BufferGeometry from THREE.js library. In this case document is undefined in three.js.
// package.json
{
...
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"react": "^16.2.0",
"react-test-renderer": "^16.2.0",
"three": "^0.89.0",
...
}
Does the issue only occur when evaluating code in debugger, or you are getting runtime errors? 'Undefined' values while debugging are often caused by sourcemaps - when variable name in generated code doesn't match the source one, and no name mappoings are provided
This issue is present in debug and run mode as well:
"^ArrayBufferHelper methods Gets Part Size$"
(node:89002) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'createElementNS' of undefined
No idea, sorry... It must be smth specific to your application.
Ok, thanks. Could you please approve this comment (created today at 10:57). Thanks.
I'm not permitted to approve comments:(
@Anis, I deleted your last comment - your first one is now approved and visible, so duplicating it just makes a confusion
I had the same problem on a project created using create-react-app and the recommended setting of "--env=jsdom" fixed it.
Wondering if you'd be better updating the instructions on https://www.jetbrains.com/help/webstorm/running-unit-tests-on-jest.html to mention this. Otherwise I presume you'll have a lot of React newcomers following the path: create-react-app, spend time setting up the Jest config in Webstorm, get the error message, end up reading this support page.
we have a feature request for settings up appropriate Jest options automatically when creating react app, https://youtrack.jetbrains.com/issue/WEB-28401. Please follow it for updates