Skip to content

Instantly share code, notes, and snippets.

@jericbas
Created June 21, 2019 05:05
Show Gist options
  • Save jericbas/11cbce3a1481b00492a42e416d92752a to your computer and use it in GitHub Desktop.
Save jericbas/11cbce3a1481b00492a42e416d92752a to your computer and use it in GitHub Desktop.
Mock `window.location.search`
it('mocks search', () => {
delete window.location;
window.location = { search: '?query=phone' };
expect(window.location.search).toEqual('?query=phone');
});
@MikolajBryksa
Copy link

Thanks. Not that easy to make it work with typescript 😅

Mock window.location.search in TypeScript:

global.window = Object.create(window);
Object.defineProperty(window, 'location', {
  value: {
    search: '?query=phone',
  },
});

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