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 · 64 comments

Comments

Projects
None yet
@kitmoovup
Copy link

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

gokulkrishh commented Jul 27, 2018

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

@ben-mckernan

This comment has been minimized.

Copy link

ben-mckernan commented Jul 27, 2018

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

@domenic

This comment has been minimized.

Copy link
Member

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.

Copy link

justinkchen commented Jul 27, 2018

This broke for me too :(

@gokulkrishh

This comment has been minimized.

Copy link

gokulkrishh commented Jul 27, 2018

@ben-mckernan Thanks 👍

@miamollie

This comment was marked as spam.

Copy link

miamollie commented Jul 27, 2018

Also experiencing this.

get localStorage() {

@domenic

This comment has been minimized.

Copy link
Member

domenic commented Jul 27, 2018

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

@miamollie

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

bencefr commented Jul 27, 2018

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

@gokulkrishh

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

ben-mckernan commented Jul 27, 2018

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

@miamollie

This comment has been minimized.

Copy link

miamollie commented Jul 27, 2018

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

@kitmoovup

This comment has been minimized.

Copy link
Author

kitmoovup commented Jul 27, 2018

@ben-mckernan solution fixed it. Thanks!

@DcsMarcRemolt

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Member

Zirro commented Jul 27, 2018

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

antonosmond added a commit to echo-health/node-grpc-interceptors that referenced this issue Nov 19, 2018

@Shreya1417

This comment has been minimized.

Copy link

Shreya1417 commented Nov 21, 2018

"jest": {
"verbose": true,
"testURL": "http://localhost/"
}
Add this code snippet in package.json file.
It worked for me.

@christian-draeger

This comment has been minimized.

Copy link

christian-draeger commented Nov 22, 2018

you can also add this to the affected test if jsdom is not needed there

/**
 * @jest-environment node
 */

it('my test', () => {
    expect(2 + 2).toBe(4);
});

simeonackermann added a commit to AKSW/building-navigator that referenced this issue Nov 28, 2018

noscripter added a commit to noscripter/mobx-react that referenced this issue Nov 30, 2018

sync with remote (#2)
* fix: jest test jsdom SecurityError

ref:  jsdom/jsdom#2304

* Add link to mobx-react-lite (mobxjs#603)

* Create a failing test for React.forwardRef

* Refs are now properly forwarded

* observer + forwardRef is now reactive

* Minor tweak, updated changelog

* Fixed weird minification issue

* Published version 5.4.0

* Fixed rollup issue, fixes mobxjs#608

* Published version 5.4.1

* Fixed issue with ForwardRef being bundled as undefined

* Published version 5.4.2

* Cleaner implementation of forwardRef

* Added open question

genejones added a commit to genejones/reportsuites.com that referenced this issue Dec 5, 2018

Update package.json
Removed old unused package that is no longer in NPM and thus causing an error.

Worked around an issue upgraded Jest was causing: jsdom/jsdom#2304

willscripted added a commit to ProdPerfect/prodperfect-keen-tracking.js that referenced this issue Dec 19, 2018

Fix localstorage not available errors in test suite
SecurityError: localStorage is not available for opaque origins
js-dom does not allow access to localStorage on the default page 'about:blank'

For details, see jsdom/jsdom#2304 (comment)

sodatea added a commit to vuejs/vue-cli that referenced this issue Dec 28, 2018

fix: set `url` option for `jsdom-global` (#3131)
This fixes `SecurityError: localStorage is not available for opaque origins` in mocha tests caused by jsdom (jsdom/jsdom#2304).

moelrobi added a commit to AminoJS/Amino.JS that referenced this issue Jan 3, 2019

Adding Jest Config.
This is due to a Bug described here: jsdom/jsdom#2304. Otherwise our Testcases will fail :(
@mscottx88

This comment has been minimized.

Copy link

mscottx88 commented Jan 8, 2019

I tested both options:

  1. Add this to the top of a test file:
/**
 * @jest-environment node
 */
  1. Add this stanza to the package.json:
"jest": {
    "testURL": "http://localhost/"
  }

Both options worked.

@fbaholli

This comment has been minimized.

Copy link

fbaholli commented Jan 26, 2019

I had it work by adding in the package.json file:

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

leosuncin added a commit to leosuncin/graphql-import-loader that referenced this issue Jan 28, 2019

feat(loader): Use relative path inside SDL
Use the file path instead of file content to import schema. Set Jest's environment to node
to avoid the error with jsdom's localStorage, see facebook/jest#6766 and jsdom/jsdom#2304.

BREAKING CHANGE: Use absolute path inside DSL won't work anymore, but now will be the same
behavior of `graphql-import` dealing with paths.

Fix prisma#10

mischah added a commit to micromata/Baumeister that referenced this issue Feb 8, 2019

fix: Resolve error running tests on Node 6
This error was caused by a minor Jest update.
See jsdom/jsdom#2304

mischah added a commit to micromata/Baumeister that referenced this issue Feb 8, 2019

fix: Resolve error running tests on Node 6
This error was caused by a minor Jest update.
See jsdom/jsdom#2304

mischah added a commit to micromata/generator-baumeister that referenced this issue Feb 18, 2019

fix(baumeister): Resolve error running tests on Node 6
This error was caused by a minor Jest update.
See jsdom/jsdom#2304
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment