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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With