Reputation:
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
Upvotes: 1
Views: 4439
Reputation: 2929
Object.defineProperty(window, 'location', { writable: true, value: { assign: jest.fn() } });
Promoting what @Sushil Kumar said. ^ this is the answer that works w/ jest 27, 28, 29.
Upvotes: 0
Reputation: 45121
You could provide your custom implementation
jest.spyOn(window.location, 'assign').mockImplementation(url => console.log(url))
Upvotes: 2