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



 

13 comments

Please can you attach a screenshot of the error?

0

Try adding

--env=jsdom

to Jest Options: field

4

That worked! Strange it didn't in the package.json, but this is much nicer than appending it in the package.

Thanks!

0

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",
    ...
}

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

0

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

0

No idea, sorry... It must be smth specific to your application.

0

Ok, thanks. Could you please approve this comment (created today at 10:57). Thanks. 

 
0

I'm not permitted to approve comments:(

0

@Anis, I deleted your last comment - your first one is now approved and visible, so duplicating it just makes a confusion

1

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.

0

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

0

Please sign in to leave a comment.