What's a good way to mock window.location? #2213
-
I tried mocking example code 👇 import { vi } from "vitest"
describe('window.location mock test', () => {
const hash = '#test-hash';
// ✅
test('is passed', () => {
expect(window.location.hash).not.toBe(hash);
});
// ✅
test('is passed', () => {
vi.stubGlobal('location', { hash });
expect(window.location.hash).toBe(hash);
});
// ❌
test('is failed', () => {
expect(window.location.hash).not.toBe(hash);
});
}); How can I mock |
Beta Was this translation helpful? Give feedback.
Answered by
sheremet-va
Oct 27, 2022
Replies: 1 comment 7 replies
-
You need to reset the state in |
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
uttk-dev
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to reset the state in
beforeEach
hook