Changing url does not work with Jest
See original GitHub issueI’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
.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:5
- Comments:27 (9 by maintainers)
Top Results From Across the Web
How to test url change with Jest - javascript - Stack Overflow
I found a working method by declaring in the beginning of the test a global variable: global.window = { location: { pathname: null...
Read more >How to test url change with Jest - splunktool
I used jsdom.reconfigure to setup different initial urls in my tests, and whenever I need to change url in code (not test), I...
Read more >Testing - React Router: Declarative Routing for React.js
Testing. React Router relies on React context to work. This affects how you can test your components that use our components.
Read more >Testing the React Router useNavigate Hook ... - LogRocket Blog
Avoid using jest.mock when making components route-aware by using the react-router useNavigate Hook, introduced in React Router v6.
Read more >Jest and URL Mocking - Ryan Doll
At some point, this stopped working based on what I believe was an update to the version of jsdom that Jest uses under...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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