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

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

我输入:

npm test

我得到:

有趣的是,import 语句在 BootScene.test.js 文件中有效,但在导入的文件中无效。

我关注的是导入语句在 JEst 下不起作用。所以我认为这可能与开玩笑使用的 Ecma Script 版本有关。所以我尝试了 解决方案,但错误仍然存​​在。

This是本题的repo/branch

当我键入 npm start 时。一切都流畅无误。

我按照 this 说明解决了问题。

谢谢 CherryDt。

错误发生是因为 jestdom 无法添加我们需要做的 canvas。对我来说,这是因为我在应用程序中使用了 lottiefiles。

这是我为我的 React 应用程序修复它的方法。

安装 jest-canvas-mock 如下。

 npm i jest-canvas-mock

然后转到设置文件,如 setup-tests.js 并导入 jest-canvas,如下所示。

import "jest-canvas-mock";

现在运行测试,应该不会再出错了。

正在安装 npm i jest-canvas-mock 并在 set-up 文件中导入导入“jest-canvas-mock”;适合我