-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Fix "localStorage is not available for opaque origins" error when using mapbox-gl-js-mock #7455
Conversation
…ng mapbox-gl-js-mock When running a test from jest and in the context it's being ran, it needs a URL to be set on the jsdom (it defaults to 'about:blank')... Reference: jsdom/jsdom#2304 I'll admit this is a bit of a workaround but here is no other way to set the options for jsdom and trying the suggestions to lock in a specific version of jsdom didn't work for me.
@ryanhamley, could you review? |
This looks like it would also require an update to |
This appears to have been fixed directly in Jest so I'm inclined to close this PR and encourage anyone seeing this issue to upgrade Jest to at least version Notes on bug: Jest issue: jestjs/jest#6766 |
@ryanhamley thanks for the investigation... I can understand why you would say that but the create-react-app repro is already using Jest 23.6.0. Since mapbox-gl is using jsdom@11.11.0, it doesn't have the problem, the bug won't be seen but upgrading jsdom you'll run into the bug (which mapbox-gl would presumably do at some point). After upgrading
The error will occur:
Also I noticed the original PR would need to be updated since JSDOM is instantiated twice, not just once. And... after applying the fix I had suggested in PR, it doesn't look like it would pass the unit tests (and perhaps why jsdom was locked in at 11.11.0 for mapbox-gl).
Anyway... you're right gl-js-mock is bumping jsdom. Maybe I can get gl-js-mock to lock in jsdom at 11.11.0. PR created here: mapbox/mapbox-gl-js-mock#29 |
If there is no intention to upgrade jsdom for mapbox-gl-js, does anyone know the maintainer of mapbox-gl-js-mock so you can walk ever there and approve that PR? The repos appears inactive with unanswered issues/PRs. |
Updating in both places makes the error go away but introduces the problems with the RTL plugin as you note. The issue seems to be that setting the window URL changes the pluginURL from
becomes
But I'm very wary of changing the library's window implementation's base URL. cc @ChrisLoer |
Apparently the root cause of the issue is that some code is copying JSDOM |
Aha! It looks like you're right but it looks like mapbox-gl itself is the one doing the copying, reference: https://github.com/mapbox/mapbox-gl-js/blob/master/src/util/window.js#L72 https://github.com/mapbox/mapbox-gl-js/blob/master/src/util/util.js#L153-L160 Relevant lines of the stack trace here:
And, more specifically, |
I'm trying to understand Seeing 2 problems total, one is that And, that it is also doing the So, if you can validate this thinking... A few things... it seems like we can't just do So.... it could be an acceptable trade off to only set the URL for the JSDom. And just a note that my PR only addressed the JSDom where it is instantiated the first time (it is actually instantiated twice) so if my pr is re-opened then that'd need to be addressed along with the broken tests. |
So after doing some digging and research of my own, I have a better grasp of what's happening here. Interestingly, given JSDOM's explanation, I don't think the error is caused specifically by copying JSDOM properties to the Node The upshot of all of this is that as long as our tests are passing, I think adding the URL to JSDOM in that file is safe to do. I'm going to reopen the pull request. Edit: Note that you actually only need to add the URL to the second instantiation of JSDOM because it's copied over to the first one. cc @ChrisLoer @mourner Am I missing any reason we shouldn't add a URL to the JSDOM implementation? |
@ryanhamley Looks like jsdom can be version bumped to the latest too (jsdom@^12.2.0). After running the unit tests, it fails on the same tests as noted earlier in this thread ( |
Setting |
So a somewhat hacky solution I found is this:
It avoids the problems of setting a base URL for JSDOM and allows us to continue upgrading JSDOM. Anything that uses |
Solid solution! It's even partly in line with the original intention of deleting the props off window at the top of restore. Thumbs up. |
When running a test from jest and in the context it's being ran, it needs a URL to be set on the jsdom (it defaults to 'about:blank')...
Reference to a lengthy discussion about it: jsdom/jsdom#2304
I'll admit this is a bit of a workaround but here is no other way to set the options for jsdom and trying the suggestions to lock in a specific version of jsdom didn't work for me (and the other suggestions for changing jest didn't matter since jsdom is instantiated from this changed file).
Here is a reproduction based off create-react-app:
https://github.com/fc/mapbox-gl-js-mock-repro
Here is the test:
https://github.com/fc/mapbox-gl-js-mock-repro/blob/master/src/App.test.js
Run this command after a yarn install:
Error output is: