61

This is my __tests__/App.js file:

import React from 'react';
import ReactDOM from 'react-dom';
import App from '../src/containers/App';

it('renders without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render(<App />, div);
});

// sanity check
it('one is one', () => {
  expect(1).toEqual(1)
});

And this is the output I get when running yarn test:

FAIL  __tests__/App.js
  ● renders without crashing

    ReferenceError: document is not defined

      at Object.<anonymous>.it (__tests__/App.js:6:15)

  ✕ renders without crashing (1ms)
  ✓ one is one

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        0.128s, estimated 1s
Ran all test suites related to changed files.

Do I need to import another module in order for document to be available here?

Thanks for the help!

6
  • 2
    document isn't available in a node environment. you should check out enzyme for testing react components airbnb.io/enzyme Commented May 10, 2017 at 20:03
  • 1
    you could also use karma for testing karma-runner.github.io/1.0/index.html Commented May 10, 2017 at 20:04
  • 1
    I can't answer specifically as I've never tried to use the document object in Jest tests, but github.com/airbnb/enzyme is generally the goto tool for testing React components and is definitely worth checking out. Commented May 10, 2017 at 20:05
  • great - needed to use enzyme to get it working! thx all. Commented May 10, 2017 at 20:11
  • 1
    Possible duplicate of error 'document' is not defined : eslint / React Commented Aug 4, 2017 at 8:24

11 Answers 11

70

For me either of these worked

In package.json file adding test script env flag

"scripts": {
   "test": "react-scripts test --env=jsdom"
}

Using enzyme

 import { shallow } from 'enzyme';

 it('renders without crashing', () => {
  shallow(<App />);
 });
Sign up to request clarification or add additional context in comments.

Comments

50

I set the value in jest.config.js to below and it worked:

testEnvironment: 'jsdom'

4 Comments

Thanks. I like this answer cause I don't have to edit package.json myself.
I think this is the best answer!
this is the best answer because use the jest config file to set up this environment, brilliant.
This worked, just make sure you have jest-environment-jsdom.
28

Placing the comment at the top of your unit-test source

/**
 * @jest-environment jsdom
 */

signals jest to mock document and window.

Comments

17

In the package.json if using jest

"scripts":{
      "test": "jest --env=jsdom"
}

Comments

7

In my case, the solution was:

  1. Add field testEnvironment: "jsdom" to jest.config.js
  2. npm i jest-environment-jsdom -D

Comments

3

I have just run into this issue, September 2021. I am learning testing in react and I resolved this issue by entering node_modules folder. Inside that folder there is a jest folder. Inside jest folder there is a package.json file. Inside that package.json file I inserted one thing. Similar to what someone mentioned above.

package.json file

"testEnvironment": "jsdom"

I know generally I should not edit node_modules folder, but just for thesake of continuing to learn this will have to do.

I do not know whether some other solution would be necessary for a more serious project.

2 Comments

It would be better to add this in the package.json under the jest object
Yes, William, I found that solution later. Thanks
2

put this on top of your index.test.js ( or test file). Don´t put it on the middle or end.

/** * @jest-environment jsdom */

Comments

1

In my case, I am using Mocha instead of Jest and I had to assign manually jsdom to the global.document. May not sound like a perfect solution but it's any.

In .mocharc.yml add a path to the file jsdom-loader.js:

require: ['@babel/register',
          'test/jsdom-loader']

jsdom-loader.js

const { JSDOM } = require('jsdom');
const environment = new JSDOM('');
const { document } = environment.window;
global.document = document;

1 Comment

this solution is working well for me in jest and allowing me to run server side tests next to react component tests in the same jest setup.
0

If you are using create-react-app, make sure to run yarn test instead of yarn jest or something else.

Comments

0

I solved the problem by changing the file extension from myfile.test.ts => myfile.test.tsx ... in addition to the above solutions. Indeed, I had a TS error with @testing-library/react at render(<MyComponent/>) in my .test.ts file, and by changing the extension all problems were solved.

Comments

0

I had same problem in vitest. The default environment in Vitest is a Node.js environment. If you are building a web application, you can use browser-like environment through either jsdom or happy-dom instead. doc There are three ways you can add env for your test:

  1. In package.json file add
{
   "scripts":{
       "test": "vitest --environment=jsdom"
   }
}
  1. In vitest.config.ts file add:
export default defineConfig({
   environment: 'jsdom'
})
  1. Add the below code at the top of your test file
/**
 * @vitest-environment happy-dom
 */

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.