Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest : SecurityError: localStorage is not available for opaque origins #2304

Closed
kitmoovup opened this issue Jul 27, 2018 · 80 comments · May be fixed by drcmda/react-contextual#31
Closed

jest : SecurityError: localStorage is not available for opaque origins #2304

kitmoovup opened this issue Jul 27, 2018 · 80 comments · May be fixed by drcmda/react-contextual#31

Comments

@kitmoovup
Copy link

@kitmoovup kitmoovup commented Jul 27, 2018

When I run a jest with my test cases. It shows the following error when I upgrade the package. In my test cases, there is no localStorage is used. How can I fix this problem?

 SecurityError: localStorage is not available for opaque origins
      
      at Window.get localStorage [as localStorage] (node_modules/jsdom/lib/jsdom/browser/Window.js:257:15)
          at Array.forEach (<anonymous>)


@domenic
Copy link
Member

@domenic domenic commented Jul 27, 2018

Summary of below discussion:

  • The fix is to set a URL for your jsdom, which might need to be done through your testing environment configuration. The default URL, of "about:blank", will cause errors when trying to access localStorage.
  • The root cause is often libraries looping over all jsdom properties and adding them to the global; even if you never use localStorage in your tests, some library or test framework you are depending on is "using" it in this manner. Note that this looping-and-copying technique is explicitly unsupported, so it's not surprising that code that does it could break in a minor release.

Transcription of below comment on the question of whether this is a "breaking change", which has since been hidden by GitHub's "see more comments" feature:

Thanks, but as of this time I don't believe that's the best path forward. It isn't a breaking change for jsdom to implement new web platform features; that's a minor version.

It's possible for dependents, even widely-used dependents, to write unfortunate code that detects new features and throws an exception if they appear. I hope we can all agree that even if such dependents exist, jsdom should not forever give up on adding new features in minor releases.

The situation here is not quite that drastic, but seems to be similar. I haven't seen anyone track down the offending Jest code yet, so it's hard to say exactly, but it appears they're performing an operation which is explicitly broken by the normal course of jsdom development, and which would not work in a web browser that implements localStorage.

I realize this is a hard situation for those of you impacted, by no fault of your own, by an unfortunate interaction regarding how your direct dependency is (ab)using your indirect dependency. But I'm hopeful this can be addressed at the right level, of fixing Jest's buggy code, instead of removing a useful feature from all jsdom users due to one dependent's bugs.

Original reply to the OP, for context:

It seems likely you are not setting a URL in your tests, but you, or perhaps Jest, are accessing window.localStorage. The Jest maintainers may know more about the best fix, but I hear there is a way to set URLs in your Jest tests?

@dmitrizzle
Copy link

@dmitrizzle dmitrizzle commented Jul 27, 2018

This is a very recent issue that seems to have popped up with 11.12.0. I get these same errors when using jest with enzyme.

@ben-mckernan
Copy link

@ben-mckernan ben-mckernan commented Jul 27, 2018

Yes, I started having the issue after upgrading from 11.11.0 to 11.12.0. Setting testURL in the jest config fixes the issue.

@gokulkrishh
Copy link

@gokulkrishh gokulkrishh commented Jul 27, 2018

@ben-mckernan Hi, What's the URL you gave to fix this ??

@ben-mckernan
Copy link

@ben-mckernan ben-mckernan commented Jul 27, 2018

@gokulkrishh I set it to http://localhost/ but any valid URL seems to work.

@domenic
Copy link
Member

@domenic domenic commented Jul 27, 2018

I guess this is probably Enzyme-specific, because Enzyme does the thing that the jsdom docs explicitly warn not to do? Not too surprising that broke on a minor release, unfortunately :(

@justinkchen

This comment was marked as spam.

@gokulkrishh
Copy link

@gokulkrishh gokulkrishh commented Jul 27, 2018

@ben-mckernan Thanks 👍

@miamollie

This comment was marked as spam.

@domenic
Copy link
Member

@domenic domenic commented Jul 27, 2018

("+1" comments will be marked as spam; emailing everyone in the issue thread is not helpful.)

@miamollie
Copy link

@miamollie miamollie commented Jul 27, 2018

Apologies. Just trying to help.

Can confirm adding testURL to jestConfig as @ben-mckernan suggested did fix it.

And we are using Enzyme too, if that helps confirm your intuition.

@bencefr
Copy link

@bencefr bencefr commented Jul 27, 2018

For electron app test I just set it to "file:/" which also works.

@gokulkrishh
Copy link

@gokulkrishh gokulkrishh commented Jul 27, 2018

@miamollie I added testURL as per @ben-mckernan suggestion (Using Jest + Enzyme, not sure its enzyme related. Error coming from jest-environment-jsdom which uses jsdom). Due to that my some other test files are failing. Just FYI. See if its works for you. Your test cases might be diff from mine (TestURL might work for you).

@ben-mckernan
Copy link

@ben-mckernan ben-mckernan commented Jul 27, 2018

@domenic I am only using jest. So I'm not sure that it is an enzyme issue.

@miamollie
Copy link

@miamollie miamollie commented Jul 27, 2018

@gokulkrishh Yeah, same, it stopped the localStorage security error but made some other tests fail.

@kitmoovup
Copy link
Author

@kitmoovup kitmoovup commented Jul 27, 2018

@ben-mckernan solution fixed it. Thanks!

@DcsMarcRemolt
Copy link

@DcsMarcRemolt DcsMarcRemolt commented Jul 27, 2018

@ben-mckernan I'm using jest in an angular setup (with jest-preset-angular), same bug, same solution. So it is not an enzyme problem.

@csvan
Copy link

@csvan csvan commented Jul 27, 2018

Seems like Jest needs to change the default value of testURL for this to be mitigated (currently it is about:blank).

@gokulkrishh
Copy link

@gokulkrishh gokulkrishh commented Jul 27, 2018

@DcsMarcRemolt I was just debugging the issue. Jest is using a dependency module called jest-environment-jsdom in its package.json --> "jsdom": "^11.5.1" caret(^) because of this npm have installed jsdom as 11.12.0 (which is new version published today). So it broke for most of the users. Issue is already created in jest and linked here. Watch out for it.

@Zirro
Copy link
Member

@Zirro Zirro commented Jul 27, 2018

I'll reopen this issue to give it more visibility. See the first comment from Domenic for a solution.

asteffey added a commit to asteffey/react-nbsp that referenced this issue Dec 8, 2019
Update yarn.lock with `yarn upgrade`.

Upgrade to webpack-cli^3.1.1 to remove vulnerability from
yarn audit and address
webpack/webpack#8082
which caused build to fail after `yarn upgrade`.

Add `testURL` to jest configuration to address
jsdom/jsdom#2304 which caused
tests to fail after `yarn upgrade`.
D-Hallock223 added a commit to D-Hallock223/React-native-nfc-mangment that referenced this issue Dec 27, 2019
anandaroop added a commit to artsy/rosalind that referenced this issue Dec 31, 2019
yarn upgrade-interactive --latest (and accept all green updates)

This also necessitated a fix for a Jest/JSDom issue

facebook/jest#6769
jsdom/jsdom#2304
anandaroop added a commit to artsy/rosalind that referenced this issue Dec 31, 2019
yarn upgrade-interactive --latest (and accept all green updates)

This also necessitated a fix for a Jest/JSDom issue

facebook/jest#6769
jsdom/jsdom#2304
anandaroop added a commit to artsy/rosalind that referenced this issue Dec 31, 2019
yarn upgrade-interactive --latest (and accept all green updates)

This also necessitated a fix for a Jest/JSDom issue

facebook/jest#6769
jsdom/jsdom#2304
JohnPhoto pushed a commit to nordnet/so-fetch that referenced this issue Jan 3, 2020
dvvanessastoiber added a commit to phovea/phovea_core that referenced this issue Jan 28, 2020
* temporally remove `preset` option
* add `testURL` to avoid security error (See jsdom/jsdom#2304 (comment))
lgandecki pushed a commit to TheBrainFamily/wait-for-expect that referenced this issue Feb 6, 2020
* fix: add Promise<void> return type for expectation

Since 0.6.0 promises and async/await are supported and tested, but the return type of the expectation function did not yet reflect this change.

* fix SecurityError when running tests

Jest fails the tests with “SecurityError: localStorage is not available for opaque origins”. Setting the Jest configuration option "testURL" to "http://localhost/" fixes this (also see: jsdom/jsdom#2304 (comment)).
It’s probably a good idea to upgrade dependencies and remove this option afterwards, since with the current release of Jest this option is no longer required (it defaults to "http://localhost/“ instead of “about:blank”).
@lecstor
Copy link

@lecstor lecstor commented Feb 29, 2020

if you're not using jsdom then you'll want to set the jest config property testEnvironment to node. (no need to touch testURL)
https://jestjs.io/docs/en/configuration#testenvironment-string

thdk added a commit to thdk/mobx-router that referenced this issue Apr 11, 2020
Jest failed with errors like:  SecurityError: localStorage is not available for opaque origins

Fix from: jsdom/jsdom#2304 (comment)
@ghost
Copy link

@ghost ghost commented May 1, 2020

For anyone looking for the actual fix, it's new JSDOM('', { url: 'https://localhost' })

springer12 added a commit to springer12/microbundle that referenced this issue May 3, 2020
arnorhs added a commit to KosmosOgKaos/react-reveal that referenced this issue May 4, 2020
- Ran https://github.com/reactjs/react-codemod => rename-unsafe-lifecycles
- Replaced package-lock with yarn.lock
- updated scripts section
- fixed a broken jest test caused by not including a testURL for jsdom
  see: jsdom/jsdom#2304 for more information
@pavelsust
Copy link

@pavelsust pavelsust commented Jul 16, 2020

Try using this in your package.json file

"jest": {
"verbose": true,
"testURL": "http://localhost/"
}

@sam17896
Copy link

@sam17896 sam17896 commented Jul 20, 2020

Why setting url doesn't work for me... i am using react-native.. is there anything else that i am missing?

@zuccha
Copy link

@zuccha zuccha commented Jul 23, 2020

Why setting url doesn't work for me... i am using react-native.. is there anything else that i am missing?

We had the same issue. In our application we had this code

const { JSDOM } = require('jsdom');
const jsdom = new JSDOM('<!doctype html><html><body></body></html>');

Turns out we had to add the URL to the JSDOM constructor

const { JSDOM } = require('jsdom');
const jsdom = new JSDOM('<!doctype html><html><body></body></html>', {
  url: 'http://localhost/',
});

That fixed the issue.

@Flouwrian
Copy link

@Flouwrian Flouwrian commented Aug 6, 2020

Why setting url doesn't work for me... i am using react-native.. is there anything else that i am missing?

We had the same issue. In our application we had this code

const { JSDOM } = require('jsdom');
const jsdom = new JSDOM('<!doctype html><html><body></body></html>');

Turns out we had to add the URL to the JSDOM constructor

const { JSDOM } = require('jsdom');
const jsdom = new JSDOM('<!doctype html><html><body></body></html>', {
  url: 'http://localhost/',
});

That fixed the issue.

This worked for me thank you so much! putting the url in the jest config doesnt seem to work with react-native. putting the url in de jsdom constructor did the trick.

@gustawdaniel
Copy link

@gustawdaniel gustawdaniel commented Sep 7, 2020

Update jest from 22 to 26 fixed problem.

@Sroy0202
Copy link

@Sroy0202 Sroy0202 commented Oct 5, 2020

simply use the latest version of jest. currently I'm using 26.5.0 in year 2020 and my problem is solved

@adi518
Copy link

@adi518 adi518 commented Dec 22, 2020

Try using this in your package.json file

"jest": {
"verbose": true,
"testURL": "http://localhost/"
}

That testURL is the default URL, which means it doesn't solve the issue, at least not with Jest 26.x. I had to do what @zuccha did to get around it.

QMalcolm added a commit to QMalcolm/rocket-ui-test that referenced this issue Apr 8, 2021
Running the tests currently results in an error. Specifically
you'll see: "SecurityError: localStorage is not available for
opaque origins". The root of this issue is
[jsdom](jsdom/jsdom#2304 (comment)).
The solution is to simply set the jest `testUrl` to your localhost.
QMalcolm added a commit to QMalcolm/rocket-ui-test that referenced this issue Apr 8, 2021
Running the tests currently results in an error. Specifically
you'll see: "SecurityError: localStorage is not available for
opaque origins". The root of this issue is
[jsdom](jsdom/jsdom#2304 (comment)).
The solution is to simply set the jest `testUrl` to your localhost.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.