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:
- Install latest Jest
- Make a clean yarn/npm install
- Setup a basic test case
- 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
- Jest config adjustmenet to address recent JSDom issue A recent Renovate merged Jest update caused all tests to start to fail, due to JSDom security errors. This config adjustment was recommended in t... — committed to apollographql/apollo-client by hwillson 6 years ago
- Added testURL because of https://github.com/facebook/jest/issues/6766 — committed to microstates/react by taras 6 years ago
- build(jest): Add testURL Jest config See https://github.com/facebook/jest/issues/6766 — committed to mbfisher/seek-style-guide by mbfisher 6 years ago
- build(jest): Add testURL Jest config See https://github.com/facebook/jest/issues/6766 — committed to mbfisher/sku by mbfisher 6 years ago
- fix(jest): Add testURL to consumer Jest config See https://github.com/facebook/jest/issues/6766 — committed to mbfisher/sku by mbfisher 6 years ago
- Fix jest issue after jsodm upgrade See https://github.com/facebook/jest/issues/6766 — committed to wix/Detox by yershalom 6 years ago
- Fix JSDom 11.12 dependency issue * Causing an error: SecurityError: localStorage is not available for opaque origins * Fix as per: https://github.com/facebook/jest/issues/6766 with jest configuration... — committed to simone-smith/donate-period by CLTPayne 6 years ago
- fix(jest): Add testURL Jest config (#151) See https://github.com/facebook/jest/issues/6766 — committed to seek-oss/sku by mbfisher 6 years ago
- Set jest test environment https://github.com/facebook/jest/issues/6766#issuecomment-408381957 — committed to sloria/jrnl-parse by sloria 6 years ago
- build(jest): Add testURL Jest config (#522) See https://github.com/facebook/jest/issues/6766 — committed to seek-oss/seek-style-guide by mbfisher 6 years ago
- build(jest): Add testURL Jest config See https://github.com/facebook/jest/issues/6766 — committed to seek-oss/sku by mbfisher 6 years ago
- fix(jest): Add testURL to consumer Jest config See https://github.com/facebook/jest/issues/6766 — committed to seek-oss/sku by mbfisher 6 years ago
- chore(jest): fixes failing builds for new jsdom Reference https://github.com/facebook/jest/issues/6766 — committed to stalniy/casl by stalniy 6 years ago
- chore(jest): fixes new jsdom & jest integration issue Ref https://github.com/facebook/jest/issues/6766 — committed to stalniy/bdd-lazy-var by stalniy 6 years ago
- Reconfigure tests after updating to jsdom 11.12.0 jsdom v11.12.0 added support for localStorage apis. Due to that, it is now a requirement to set an origin url on the jsdom instance if you at all acc... — committed to gustavnikolaj/fetchception by gustavnikolaj 6 years ago
- fixing annoying jsdom bug see https://github.com/facebook/jest/issues/6766 — committed to rithmschool/LinkedList by mmmaaatttttt 6 years ago
- Add jest testURL See jsdom/jsdom/issues/2304 and facebook/jest/issues/6766 for more context. — committed to KevinGrandon/fusion-cli by KevinGrandon 6 years ago
- Add jest testURL See jsdom/jsdom/issues/2304 and facebook/jest/issues/6766 for more context. — committed to KevinGrandon/fusion-cli by KevinGrandon 6 years ago
- fix: workaround to known Jest error getting thrown: https://github.com/facebook/jest/issues/6766 — committed to boltdesignsystem/bolt by sghoweri 6 years ago
- fix: set jsdom test url for local storage support This is a workaround for issue [#6766](https://github.com/facebook/jest/issues/6766) and mentioned in [#2304](https://github.com/jsdom/jsdom/issues/2... — committed to byCedric/semantic-release-expo by byCedric 6 years ago
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 enviromentAs 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 installedjsdom
as11.12.0
(which is new version published today). Hence the issueHmm. Defining
testURL: 'http://localhost'
globally in Jest’s config doesn’t help me. But definingtestURL
in the specific project helps. Is it probably issue when you haveprojects: []
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:Then created the
jest.setup.js
and used the following scripts there:Then made
.babelrc
-file and wrote it like this:Note: “targets” is probably irrelevant here
And last but not the least, in the
package.json
I declaredscripts -> test
-node as follows:Note: I did not install
jest-environment-jsdom
or use older versions of Jest or Enzyme.Cheers!
Changing Jest’s default might make sense?
https://github.com/facebook/jest/blob/fb2a6ac4d3f4e9530941e24030b52f3d506aa2de/packages/jest-config/src/defaults.js#L75
/cc @cpojer @aaronabramov @mjesun @thymikee @rickhanlonii
@thymikee thanks for clarifying! I had trouble sorting that out from the long thread here heheh.