Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: It looks like you called `mount()` without a global document being loaded

Tags:

reactjs

enzyme

I'm trying to mount a component for testing with enzyme, and get this error.

like image 679
cameronroe Avatar asked May 08 '16 19:05

cameronroe


People also ask

Does React 18 work with enzymes?

A few days ago, React released version 18, which is not compatible with Enzyme. Furthermore, it probably is not achievable to use Enzyme with React 18. If you're still using Enzyme, it is time to look into alternatives. The most popular option besides Enzyme seems to be React Testing Library.


1 Answers

npm install --save-dev --save-exact jsdom jsdom-global

Then:

import 'jsdom-global/register'; //at the top of file, even before importing React

More info here.

Alternative: In case you are using jest with enzyme-

In your package.json, specify the test environment for jest as jsdom as follows:

"jest": {
  "testEnvironment": "jsdom"
}

I faced the same issue and it worked well for me.

like image 73
Peter Avatar answered Sep 20 '22 15:09

Peter