Skip to content

Instantly share code, notes, and snippets.

@ricokareem
Forked from remarkablemark/README.md
Created November 10, 2020 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricokareem/5ad24aebb3ecc4dfc1b8e63f7b38bfa0 to your computer and use it in GitHub Desktop.
Save ricokareem/5ad24aebb3ecc4dfc1b8e63f7b38bfa0 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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment