我在使用 Jest 测试 JavaScript 文件时遇到了困难,该文件封装了与 ThreeJS 的大量交互。我首先尝试不模拟 ThreeJS,但它不起作用:
? TestSuite › Should instantiate a renderer attached to a specific element of the DOM
TypeError: Cannot read property 'getExtension' of null
36 | */
37 | constructor(containerId: string) {
> 38 | this.renderer = new WebGLRenderer({ antialias: true });
39 | this.attachRenderer(containerId);
40 | this.createCamera();
41 | this.createScene();
Run Code Online (Sandbox Code Playgroud)
这是正常的,因为我们在没有 webgl context 的类似浏览器的环境中进行测试。所以我为解决这个问题所做的就是模拟three.js。
然后我嘲笑我的外部模块 jest.mock("three");
? TestSuite › Should instantiate a renderer attached to a specific element of …Run Code Online (Sandbox Code Playgroud)