Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  192] [ 5]  / answers: 1 / hits: 43881  / 7 Years ago, wed, may 10, 2017, 12:00:00

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!


More From » reactjs

 Answers
16

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 />);
});

[#57831] Sunday, May 7, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dakotahs

Total Points: 605
Total Questions: 104
Total Answers: 113

Location: Hungary
Member since Wed, Nov 9, 2022
2 Years ago
;