Vscode-jest: Getting `ReferenceError: window is not defined`

Created on 7 Nov 2017  ยท  3Comments  ยท  Source: jest-community/vscode-jest

Any ideas on fixes or how I can continue to troubleshoot this? It works when I run my tests in the console.

Environment

  1. node -v: 8.6.0
  2. npm -v: 5.5.1

  3. Operating system: Mac OS Sierra 10.12.6

Steps to Reproduce

I have jest configured in my package.json with this:

"setupFiles": [
    "<rootDir>/config/jest/mockLocalStorage.js"
]

And in mockLocalStorage.js I defined a mockLocalStorage class and then do this: window.localStorage = new LocalStorageMock();

Expected Behavior

Run the test normally

Actual Behavior

FAIL src/containers/component.spec.js

โ— Test suite failed to run

ReferenceError: window is not defined

  at Object.<anonymous> (config/jest/mockLocalStorage.js:16:1)
      at <anonymous>

Most helpful comment

The default testEnvironment is jsdom which should provide window. Are you specifying anything different for the testEnvironment from the command line options or configuration?

All 3 comments

The default testEnvironment is jsdom which should provide window. Are you specifying anything different for the testEnvironment from the command line options or configuration?

That fixed it. I removed it previously but didn't restart vs code. It worked after restart. Thank you!

Thanks @seanpoulter this worked for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

connectdotz picture connectdotz  ยท  24Comments

dandv picture dandv  ยท  17Comments

karb0f0s picture karb0f0s  ยท  37Comments

jackfranklin picture jackfranklin  ยท  19Comments

kartheektrilochan picture kartheektrilochan  ยท  28Comments