Euridice01
Euridice01

Reputation: 2568

How to write unit tests for three.js?

Essentially, what I'm asking is, is there a way to write unit tests for my JavaScript files for three.js library?

I have a 3D viewer that houses a camera, renderer, loader, etc... How do I write tests for something like that? Is there something out there that I can read for that? Is it even possible?

Upvotes: 8

Views: 5820

Answers (2)

Amit Teli
Amit Teli

Reputation: 935

I found a way to unit test webgl/threejs in headless way. This does not need any image comparison etc.

https://github.com/AmitTeli/webgl-three-test

Approach taken:

  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)

Upvotes: 1

dres
dres

Reputation: 1211

You can either test properties of the expected state or use headless browser like chromium to render expected and actual images. If you search for testing WebGL, you will find examples of the latter

Upvotes: 0

Related Questions