Skip to content Skip to sidebar Skip to footer

Specify Window.location For Each Test File For Jest

I am upgrading to Jest 22 and I got some problem about mocking window.location. In past, this method is work fine but not work after upgraded. Object.defineProperty(window.location

Solution 1:

Solution from jest collaborator for June 2019:

delete global.window.location;
global.window = Object.create(window);
global.window.location = {
  port: '123',
  protocol: 'http:',
  hostname: 'localhost',
}

Solution 2:

There is no really nice way at the moment. As we use react-router I dent to use it for all url changes and mock it in the tests. If you don't use react-router just create a location module like this:

export default (location) => window.location = location 

that can be easily mocked in the test


Post a Comment for "Specify Window.location For Each Test File For Jest"