Jest import statement: 'TypeError: Cannot set property 'fillStyle' of null'

Viewed 6321

I type:

npm test

and I get:

enter image description here

It's interesting to note that the import statement works inside the BootScene.test.js file but it doesn't work in the imported file.

I focused on the import statement not working under JEst. So I thought maybe it has something to do with Ecma Script version jest uses. So I tried this solution but the error persists.

This is the repo/branch of this question.

When I type npm start. Everything flows swiftly and with no errors.

3 Answers

The error occurs because jestdom is not able add canvas, which we need to do. For me this happened because I was using lottiefiles for the application.

Here is how I fixed it for my react application.

Install jest-canvas-mock as below .

 npm i jest-canvas-mock

Then go to setup file like setup-tests.js and import jest-canvas as below .

import "jest-canvas-mock";

Now run test, you should not get errors any more.

I solved it by following this instructions.

enter image description here

Thanks CherryDt.

Installing npm i jest-canvas-mock and Importing import "jest-canvas-mock" in set-up file; works for me

Related