I am trying to get started with state of the art web development learning React/Redux.
Right now I am stuck at getting tests running. For some reason jest fails with
Task :frontend:test yarn jest v1.0.2 $ "/Users/gunnar/git/app.oakstair.se/frontend/node_modules/.bin/jest" FAIL src/containers/App/App.test.js ● Test suite failed to run ReferenceError: window is not defined at Object.<anonymous> (config/polyfills.js:18:1) at next (native) at process._tickCallback (internal/process/next_tick.js:109:7)
I have googled for a while without any success ...
You can force window to be undefined in some test files by adding @jest-environment node at the top of the file.
You can run Jest directly from the CLI (if it's globally available in your PATH , e.g. by yarn global add jest or npm install jest --global ) with a variety of useful options. If you'd like to learn more about running jest through the command line, take a look at the Jest CLI Options page.
jsdom is a pure JavaScript implementation of the DOM and browser APIs that runs in node. If you're not using Jest and you would like to run your tests in Node, then you must install jsdom yourself. There's also a package called global-jsdom which can be used to setup the global environment to simulate the browser APIs.
I had the same issue and I do not believe modifying globals is the way to do it. The issue was because in my jest config I had testEnvironment
set to node
when it should've been jsdom
. For me this setting was located in package.json as defined by the react starter app.
In your package.json add window
like global something like this
"jest": { "verbose": true, "preset": "react-native", "setupFiles": ["./jest/setup.js"], "testRegex": "(/tests/.*|\\.(test|spec))\\.(ts|tsx|js)$", "transformIgnorePatterns": [ "node_modules/(?!(jest-)?react-native|lottie-react-native)" ], "globals": { "window": {} } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With