Jest: this._document.origin is undefined

Created on 26 Sep 2018  ·  12Comments  ·  Source: facebook/jest

🐛 Bug Report

When trying to use shallow on a component, I get TypeError: Cannot read property 'origin' of undefined at Window.get localStorage. I have ComponentWillMount that is calling to a service that saves to local storage. The trace prints out an issue with JSDOM.

node_modules/jsdom/lib/jsdom/browser/Window.js:256:26

To Reproduce

const wrapper = shallow(

System:
    OS: macOS 10.14
    CPU: x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
  Binaries:
    Node: 10.3.0 - /usr/local/bin/node
    Yarn: 1.7.0 - /usr/local/bin/yarn
    npm: 6.1.0 - /usr/local/bin/npm
  npmPackages:
    jest: 23.6.0 => 23.6.0
Needs More Info

Most helpful comment

Hi there. I'm getting these too...

Kind of randomly... After removed node_modules and reinstalling again it stopped for a while, then again.

My project uses localForage (for IndexedDB) and localStorage but this error comes up in unrelated files. I mean: the tests that fail and show this message are not related to localStorage at all.

I'd guess its a JSDOM error but since googling brought me straight here and it's the same error just wanted to add as a +1

All 12 comments

Can you include the reproduction steps? Seems like they got cut out

Looks like they did.

I’m simply just using shallow(MyComponent).

What's MyComponent? Please include a more complete example :)

MyComponent is just a component I built that needs to be shallow copied.

In it I call a library for that I believe sets something in LocaLStorage onComponentDidMount.

The test just die because of the aforementioned error just have shallow copying the component, no other code.

Please provide a reproduction we can pull down, run and see the error. I _highly_ doubt this is an issue with Jest, but if it is I'm happy to reopen

Jest is _simply just_ a test runner, it works for me 🤷‍♂️

@thezachcannon i am facing the same issue..How were you able to fix this?

Hi there. I'm getting these too...

Kind of randomly... After removed node_modules and reinstalling again it stopped for a while, then again.

My project uses localForage (for IndexedDB) and localStorage but this error comes up in unrelated files. I mean: the tests that fail and show this message are not related to localStorage at all.

I'd guess its a JSDOM error but since googling brought me straight here and it's the same error just wanted to add as a +1

I'm still facing the same issue.

For me it worked to use act directly from react-dom/test-utils instead from @testing-library/react. But I really don't know why.

I now have this issue again, very odd. :/

I fixed it by using a mocking library for http requests. In my case i used jest-fetch-mock.

Was this page helpful?
0 / 5 - 0 ratings