jsdom: Changing url does not work with Jest
I’m trying to change the window url in my Jest specs and it throws an error. Not sure if this is an issue with the way that Jest has configured jsdom, or a problem with jsdom. A small example:
import jsdom from 'jsdom';
it('works', () => {
jsdom.changeURL(window, 'https://example.com?foo=butts')
});
I run it like:
$ jest --config config/jest.json spec/js/my_test.js
And get an error:
TypeError: Cannot set property '_URL' of undefined
at Object.<anonymous>.exports.changeURL (node_modules/jsdom/lib/jsdom.js:64:12)
at Object.<anonymous> (spec/js/my_spec.js:6:45)
Not really much in the way of custom configuration on our part when it comes to Jest. Using jsdom@9.9.1
and jest@17.0.3
.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 5
- Comments: 27 (9 by maintainers)
I am also having a similar issue regarding
history
(initializing a React app w/ react-router; ran via jest):I’ll try to allocate some time in setting up a test repo, without jest.
Can you produce a version which does not add properties to the global object? That isn’t a supported configuration. See https://github.com/tmpvar/jsdom/wiki/Don’t-stuff-jsdom-globals-onto-the-Node-global
@mockdeep , not sure, I am trying to trigger a page change using react-router within a unit test. I have the exact same version as you and I have tried your code into my env and it did the same, helpers like
parseURLToResultingURLRecord
doesn’t get called properly like a real browser…You can also reproduce it like with this in your test:
Please for anyone experiencing this, produce some minimal (~10 line) example that uses only jsdom APIs (not Jest APIs) so we can debug it and add it to our test suite.
This is caused by having more than one version of
idlUtils.implSymbol
floating around.I think this has to do with jest running some variant of
in an attempt to create a sandbox environment, but breaks stuff since
Symbol('impl') !== Symbol('impl')
. I’m not certain, though