ecmascript-6jestjssinon

window.location.assign() not implemented in Jest JSDom


I have the function

 window.location.assign(url)

which does not exist in jsdom... so raising an error in my test

Someone mention the possibility to stub this function ( using Sinon) see jsdom issue

 sinon.stub(window.location, 'assign');
 expect(window.location.assign).to.have.been.calledWith(url);

How can I replicate it using Jest ? ( since it does not exist , I cannot use spies... it has to be mocked )

thanks for feedback


Solution

  • You could provide your custom implementation

    jest.spyOn(window.location, 'assign').mockImplementation(url => console.log(url))