16

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.

1
  • I got such an error in our own testing codebase, and was able to fix it by creating a mock implementation following this answer: stackoverflow.com/a/56999581 Commented Apr 21, 2023 at 7:03

2 Answers 2

17

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);
Sign up to request clarification or add additional context in comments.

Comments

10

This was an issue in jest-canvas-mock that was fixed in 2.4.0.

Upgrading to 2.4.0 or later should resolve the issue, and allow use of node 18 or better.

2 Comments

I can not confirm this. Got 2.4.0 and it is still an issue.
I added npm install jest-canvas-mock@latest --legacy-peer-deps to my CI pipeline and that did fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.