且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用Jest单元测试ThreeJS应用程序

更新时间:2023-11-22 17:44:52

我也在webgl上工作,并通过以下方式解决了这个问题. https://github.com/AmitTeli/webgl-three-test

I am also working on webgl this and solved this in following way. https://github.com/AmitTeli/webgl-three-test

该方法的摘要是

  1. 将所有全局变量(例如场景渲染器相机)移动到 index.html
  2. 在加载页面时将其初始化.例如在此reactjs示例中,它们在根组件的 componentDidMount()中初始化.其他功能可以使用此上下文.(您可以通过运行 yarn start 进行检查)
  3. 在进行单元测试时,请在 setupTest.js 中对其进行初始化.我们将需要无头Webgl的其他开发依赖项- gl canvas
  4. 现在使用此设置,我们可以在 App.test.js 中运行测试(您可以通过运行 yarn test 进行检查)
  1. Move all the global variables like scene, renderer and camera to index.html
  2. Initialize them on loading of the page. e.g. in this reactjs example, they are initialized in componentDidMount() of the root component. Other functions can use this context. (You can check this by running yarn start)
  3. While doing the unit testing, initialize them in setupTest.js. We will need additional dev dependencies of headless webgl - gl and canvas
  4. With this setup now we can run the test in say App.test.js (You can check this by running yarn test)