jest: JSDom 11.12 causes SecurityError: localStorage is not available for opaque origins

🐛 Bug Report

A clear and concise description of what the bug is.

Jsdom just recently upgraded to version 11.12, which includes support for window.localStorage and other features. See https://github.com/jsdom/jsdom/blob/master/Changelog.md.

A fresh yarn install deduces that Jest uses this latest version, but this causes all existing Jest test cases to fail with:

SecurityError: localStorage is not available for opaque origins

To Reproduce

Steps to reproduce the behavior:

  1. Install latest Jest
  2. Make a clean yarn/npm install
  3. Setup a basic test case
  4. Run it

Expected behavior

Test case should pass.

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: Linux 4.15
    CPU: x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
  Binaries:
    Node: 10.7.0
    Yarn: 1.7.0
    npm: 6.1.0
  npmPackages:
    jest:
      wanted: ~23.4.0
      installed: 23.4.1

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 165
  • Comments: 22 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Update: as noted in the linked issue, adding the following to the Jest config solves the issue:

testURL: 'http://localhost'

Perhaps the default for this config option needs to be changed?

Add testEnvironment env config for non-browser enviroment

  "jest": {
    "testEnvironment": "node"
  },

As a temporary workaround you can install jsdom "11.11.0" (exact) as a dev-dependency in your package. jest-environment-jsdom should then use this version instead of the lastest "11.12.0" causing the behavior.

Cheers!

Jest 23.5.0 has been released with the updated default testURL

@csvan @SamuelBrucksch I was just debugging the issue. Jest is using a dependency module called jest-environment-jsdom in itspackage.json --> "jsdom": "^11.5.1" caret(^) because of this npm have installed jsdom as 11.12.0 (which is new version published today). Hence the issue

Hmm. Defining testURL: 'http://localhost' globally in Jest’s config doesn’t help me. But defining testURL in the specific project helps. Is it probably issue when you have projects: [] in Jest config?

@pgonzal fix has been merged to master: https://github.com/facebook/jest/pull/6792. Kindly wait for the release to happen 😃

I had same error.

This fix seems to work. However when i run my tests from Webstorm i always get empty test suite now. Looks like there are also issues now with the Jest plugin in Webstorm when adding a testURL.

I solved the problem with a following methods:

Create jest.config.js and place the following code there:

module.exports = {
  testEnvironment: 'node',
  verbose: true,
  setupFiles: ['<rootDir>/jest.setup.js'],
  testPathIgnorePatterns: ['<rootDir>/node_modules/']
};

Then created the jest.setup.js and used the following scripts there:

import {configure} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
// More settings if needed
configure({ adapter: new Adapter() });

Then made .babelrc -file and wrote it like this:

{
  "presets": [
    "react",
    [
      "env",
      {
        "targets": {
          "browsers": ["last 2 versions"]
        }
      }
    ]
  ]
}

Note: “targets” is probably irrelevant here

And last but not the least, in the package.json I declared scripts -> test -node as follows:

//...
"scripts": {
    "test": "jest"
},
//...

Note: I did not install jest-environment-jsdom or use older versions of Jest or Enzyme.

Cheers!

@thymikee thanks for clarifying! I had trouble sorting that out from the long thread here heheh.