Sometimes, we want to fix “ReferenceError: document is not defined” error when trying to test a create-react-app project with Jest.
In this article, we’ll look at how to fix “ReferenceError: document is not defined” error when trying to test a create-react-app project with Jest.
How to fix “ReferenceError: document is not defined” error when trying to test a create-react-app project with Jest?
To fix “ReferenceError: document is not defined” error when trying to test a create-react-app project with Jest, we can call the shallow
function to shallow mount our component for testing.
For instance, we write
import {
shallow
} from 'enzyme';
it('renders without crashing', () => {
shallow(< App / >);
});
to shallow mount the App
component with shallow with Enzyme.
Conclusion
To fix “ReferenceError: document is not defined” error when trying to test a create-react-app project with Jest, we can call the shallow
function to shallow mount our component for testing.