Skip to content
Discussion options

You must be logged in to vote

In Browser Mode I would treat window.location.assign as effectively not mockable in the old happy-dom style. Browser Mode is running in a real browser, so Location behaves like the platform object it is; vi.spyOn(location, "assign") working in DOM simulators was the less-realistic part.

For window.open, you may be able to stub/spy depending on the browser/property descriptor, but I would not build a test strategy around patching browser navigation globals.

The most Vitest-friendly pattern is to put navigation behind your own module boundary and mock/spy that module instead:

// navigation.ts
export function assign(url: string | URL) {
  window.location.assign(url)
}

export function open(url

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@thomasneger
Comment options

Answer selected by thomasneger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants