Jest unit tests : ReferenceError: window is not defined
ReferenceError: window is not defined
Rational
This can happen when you use node instead of jsdom as a test DOM.
Solution
Put this on top of your spec file to trigger the use of jsdom
/**
* @jest-environment jsdom
*/
And add the required node modules jsdom:
$ npm install jsdom --save-dev
or
$ yarn add jsdom --dev
Other problem
ReferenceError: document is not defined
Try upgrading to npm package babel-jest equal or beyond v26.1.0
Let me know in the comment below if that worked for you and if not what you did to make it work !
Recent Comments