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)

Most upvoted comments

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.

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:

window.history.pushState('page2', 'Title', '/page2.php');

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

jsdom.evalVMScript(sandbox, vm.Script(fs.readFileSync('utils.js')))

in an attempt to create a sandbox environment, but breaks stuff since Symbol('impl') !== Symbol('impl'). I’m not certain, though