Jest test throws Cannot redefine property: window
TypeError: Cannot redefine property: windowShort answer
The error means a test tried to redefine a property that is non-configurable. In a jsdom test, window itself is the usual target. Do not redefine window. Set the page URL through testEnvironmentOptions, pick the environment with testEnvironment or a docblock, and mock single properties instead of the whole window.
Fix it fast
1Find out which cause you have
Print the property descriptor for window in a test. If configurable is false, any Object.defineProperty on it throws this error.
test('window descriptor', () => { console.log(Object.getOwnPropertyDescriptor(globalThis, 'window')); });2Match what you see, then jump to its fix
- 1
The stack points at an Object.defineProperty(window, ...) or Object.defineProperty(global, 'window', ...) line in a test or a setup fileThe test redefines a non-configurable propertyFix - 2
The failing code sets window.location or window.URL to a fake addressThe test wants a different page URLFix - 3
A test or a library that expects a browser fails while the config has no testEnvironmentThe file runs in the node environmentFix
- 1
What does the error look like?
TypeError: Cannot redefine property: window
Cause 1 of 3
The test redefines a non-configurable property
1Check
MDN explains that redefining a property whose configurable flag is false throws this TypeError, and that Object.defineProperty creates non-configurable properties unless told otherwise. The Window.window property is documented as read-only.
grep -rn "defineProperty(\(window\|global\|globalThis\)" src/ jest.setup.*2Fix
Stop replacing window. Change only the specific property you need, or let the environment provide the value you were faking.
// instead of: Object.defineProperty(global, 'window', { value: fake })
// fake one property of the existing window object:
Object.defineProperty(window, 'innerWidth', { value: 500, configurable: true, writable: true });3Verify
Run the single test file.
npx jest path/to/file.test.jsConfirmed by developer.mozilla.org
Cause 2 of 3
The test wants a different page URL
1Check
The jsdom README says code inside the window cannot override the protected properties it describes, and that the url option sets window.location and document.URL. Jest passes such options through testEnvironmentOptions.
grep -n "testEnvironment" jest.config.*2Fix
Set the URL in the Jest configuration, or per file with a docblock, instead of rewriting location in the test.
// jest.config.js
module.exports = {
testEnvironment: 'jsdom',
testEnvironmentOptions: { url: 'https://example.com/' },
};
/**
* per file instead:
* @jest-environment jsdom
* @jest-environment-options {"url": "https://example.com/"}
*/3Verify
The test now sees the URL you set.
console.log(window.location.href)Confirmed by jestjs.io
Cause 3 of 3
The file runs in the node environment
1Check
The Jest docs say the default environment is node, and that testEnvironment jsdom gives a DOM. From Jest 28 the jsdom environment is a separate package.
npm ls jest-environment-jsdom2Fix
Install the package and select jsdom for the files that need a browser, rather than faking window by hand.
npm install --save-dev jest-environment-jsdom
# then in the test file:
/**
* @jest-environment jsdom
*/3Verify
typeof window is object inside the test.
npx jest path/to/file.test.jsConfirmed by jestjs.io
How do I stop it coming back?
- Fake a single window property with configurable: true instead of replacing window.
- Choose the Jest environment per file with a docblock when only some tests need a DOM.
- Reset what you changed in afterEach so one test does not leak into the next.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
Jest fails with window.matchMedia is not a function
TypeError: window.matchMedia is not a function
jsdom does not implement window.matchMedia, so any component or library that calls it breaks under Jest.
2 causes, each with a fixWeb apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fix