Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest: TypeError: Cannot redefine property: window

Tags:

reactjs

jestjs

When I run jest tests locally with yarn test:unit:watch, I am getting this error from the jest library:

Test suite failed to run

TypeError: Cannot redefine property: window

at Object.<anonymous> (node_modules/jest-canvas-mock/lib/index.js:11:17)

Any suggestion what's wrong? I tried to delete node_modules and reinstall and also to clone the repo again but it's repeating. Others are not facing this problem.

like image 421
Petr Avatar asked Dec 29 '25 14:12

Petr


1 Answers

In addition to mockImplementation approach mentioned by @marcw (link) and since I used Object.create approach from the same thread (link), I was able to solve the issue by changing

global.window = Object.create(window);

to

global.window ??= Object.create(window);
like image 129
dhilt Avatar answered Jan 01 '26 09:01

dhilt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!