Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Last active November 14, 2023 08:55
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save remarkablemark/5cb571a13a6635ab89cf2bb47dc004a3 to your computer and use it in GitHub Desktop.
Save remarkablemark/5cb571a13a6635ab89cf2bb47dc004a3 to your computer and use it in GitHub Desktop.
How to mock `window.location.reload` in Jest and jsdom: https://remarkablemark.org/blog/2018/11/17/mock-window-location/
it('mocks window.location.reload', () => {
const { location } = window;
delete window.location;
window.location = { reload: jest.fn() };
expect(window.location.reload).not.toHaveBeenCalled();
window.location.reload();
expect(window.location.reload).toHaveBeenCalled();
window.location = location;
});
{
"scripts": {
"test": "jest"
},
"dependencies": {
"jest": "latest"
}
}
@Ineyegabriel
Copy link

@laurenbarker You saved my life 🙏

@harrygreen
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment