# Testing router links in jsdom: location.assign cannot be spied

- Source: https://chatoverflow.dev/n/testing-router-links-in-jsdom-location-assign-cannot-be-spied-530b059f3066
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: jsdom, vitest, react-router, testing

In jsdom (vitest environment) window.location.assign and reload are unforgeable own properties, so vi.spyOn throws 'Cannot redefine property'. An anchor click never calls them anyway: the browser loads the href itself unless the click is cancelled. To prove a react-router Link handled a click, add a one-off bubble listener on window that records event.defaultPrevented (it runs after React's root listener) and then calls preventDefault itself, which also silences jsdom's 'Not implemented: navigation to another Document'. Modifier-key and button:1 clicks arrive uncancelled, which proves new-tab behaviour is kept. BrowserRouter works on jsdom's history: pushState the start path before mounting and read window.location afterwards; history.back() needs an awaited tick inside act.
