question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Changing url does not work with Jest

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:27 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
jozsicommented, Mar 20, 2017

I am also having a similar issue regarding history (initializing a React app w/ react-router; ran via jest):

(node:1831) TypeError: Cannot read property '_history' of null
    at Window.get history [as history] (/Users/jozsi/Dev/wealthor/node_modules/jsdom/lib/jsdom/browser/Window.js:151:79)
    at /Users/jozsi/Dev/wealthor/node_modules/history/lib/BrowserProtocol.js:93:18
    at updateLocation (/Users/jozsi/Dev/wealthor/node_modules/history/lib/BrowserProtocol.js:82:3)
    at replaceLocation (/Users/jozsi/Dev/wealthor/node_modules/history/lib/BrowserProtocol.js:92:10)
    at /Users/jozsi/Dev/wealthor/node_modules/history/lib/createHistory.js:119:15
    at /Users/jozsi/Dev/wealthor/node_modules/history/lib/createHistory.js:90:9
    at next (/Users/jozsi/Dev/wealthor/node_modules/history/lib/AsyncUtils.js:51:7)
    at loopAsync (/Users/jozsi/Dev/wealthor/node_modules/history/lib/AsyncUtils.js:55:3)
    at confirmTransitionTo (/Users/jozsi/Dev/wealthor/node_modules/history/lib/createHistory.js:80:31)
    at transitionTo (/Users/jozsi/Dev/wealthor/node_modules/history/lib/createHistory.js:100:5)
    get history() {
      return idlUtils.wrapperForImpl(idlUtils.implForWrapper(window._document)._history);
    },

I’ll try to allocate some time in setting up a test repo, without jest.

2reactions
domeniccommented, Mar 9, 2017

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found