-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Comments
Summary of below discussion:
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 |
This is a very recent issue that seems to have popped up with |
Yes, I started having the issue after upgrading from |
@ben-mckernan Hi, What's the URL you gave to fix this ?? |
@gokulkrishh I set it to |
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 :( |
This comment has been minimized.
This comment has been minimized.
@ben-mckernan Thanks 👍 |
This comment has been minimized.
This comment has been minimized.
("+1" comments will be marked as spam; emailing everyone in the issue thread is not helpful.) |
Apologies. Just trying to help. Can confirm adding And we are using Enzyme too, if that helps confirm your intuition. |
For electron app test I just set it to "file:/" which also works. |
@miamollie I added |
@domenic I am only using jest. So I'm not sure that it is an enzyme issue. |
@gokulkrishh Yeah, same, it stopped the localStorage security error but made some other tests fail. |
@ben-mckernan solution fixed it. Thanks! |
@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. |
Seems like Jest needs to change the default value of |
@DcsMarcRemolt I was just debugging the issue. |
I'll reopen this issue to give it more visibility. See the first comment from Domenic for a solution. |
We had the same issue. In our application we had this code
Turns out we had to add the URL to the JSDOM constructor
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. |
Update jest from 22 to 26 fixed problem. |
simply use the latest version of jest. currently I'm using 26.5.0 in year 2020 and my problem is solved |
That |
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.
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.
What if you can't set url to localhost? I've set the url to path so it can load other static files. but now I'm getting the localStorage error but no clue how to solve it. |
Hi all. I'm actually starting to suffer this problem after upgrading to latest Jest 28.0.0-alpha.7. Rolling back to current 27.5.1 works fine. I've noticed that on Jest 28.x , parameters to the testEnvironment have to be passed on a testEnvironmentOptions object. Particularly, the old
Interestingly enough, it also seems Jest 28.x is setting jsdom's url to I'm using Any ideas ? Am I doing something wrong ? Does version 28 have issues configuring the test URL for jsdom environments ? jsdom library used: tried both 18.1.1 and 19.0.0 |
You should report that as a bug to Jest, not here. That said, Jest defaults to localhost, and have done so for years. But you shouldn't install jsdom yourself, it comes with Regardless, if it worked with v27 and doesn't with v28 that should be reported (with a minimal reproduction) to Jest 🙂 |
Thanks, I'm actually inspecting the code, and can't see anything wrong, but will report on jest Github as well. Thanks |
Quoting from the summary up near the top, #2304 (comment):
|
i got this error and it was because i had jest 28 and jest-environment-jsdom 26. upgrading the latter to v28 and it was fixed |
Not using Jest - fix for me using const browserEnv = require('browser-env');
browserEnv({ url: 'http://localhost' }); Hopefully this saves people in the same boat as me (seemingly the road less travelled) some time! |
I still had this issue with "jest": "^29.0.3" and even with localhost url setup in the config. Any idea? thanks |
|
I also got this question, the version of jest and jest-cli are both v29.5, even I set testEnvironment and testEnvironmentOptions, it still doesn't work. Finally, I roll back those version to 27.5.1, it works. |
* Update jest version * Update babel-jest version * Use testURL: http://localhost in jest configs. * Update to jest version 23.5 This version of jest includes a fix for jsdom/jsdom#2304 * "testURL": "http://localhost" is default with jest v23.5
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?The text was updated successfully, but these errors were encountered: