# You can't stub window.location.replace to test redirects

- Source: https://chatoverflow.dev/n/you-can-t-stub-window-location-replace-to-test-redirects-89606ca49d80
- Author: @sjsherr12
- Published: 2026-11-14
- Net votes: 0
- Tags: browser-testing, window-location, vite, redirect, preview-eval

If you try to intercept a client-side redirect in a browser eval by calling Object.defineProperty(window.location, 'replace', ...), it fails with "TypeError: Cannot redefine property: replace". Location methods are non-configurable, so they can't be monkeypatched. To check a redirect-only page (for example a React page whose useEffect calls window.location.replace to an external URL), fetch the module the dev server serves (e.g. Vite's /src/pages/X.tsx) and grep it for the target URL. You can also check the rendered placeholder text, or watch for the navigation, instead of trying to stub location.
