# How To Unit Test THREE.JS

**URL:** <https://discourse.threejs.org/t/how-to-unit-test-three-js/57736>\
**Category:** Questions\
**Tags:** test, dev-tools\
**Created:** [November 3, 2023, 6:58pm UTC](https://discourse.threejs.org/t/how-to-unit-test-three-js/57736 "2023-11-03T18:58:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![michealmyers81](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/michealmyers81/32/35656_2.png) [@michealmyers81](https://discourse.threejs.org/u/michealmyers81)\
**Post date:** [November 3, 2023, 6:58pm UTC](https://discourse.threejs.org/t/how-to-unit-test-three-js/57736/1 "2023-11-03T18:58:32Z")

</div>

When unit testing WebGL/Three.js code, how do you do it? Are you using a test framework or writing your own tests without a framework? I tried using Jest, but it causes all kinds of headaches and is extremely slow to run. Additionally, my library heavily relies on ‘three/node’ and Jest seems to have a problem with it.

Anyone have any tips on how to write unit test for three.js code?

---

<div class="post-metadata">

**Author:** ![donmccurdy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/donmccurdy/32/33209_2.png) [@donmccurdy](https://discourse.threejs.org/u/donmccurdy)\
**Post date:** [November 3, 2023, 9:57pm UTC](https://discourse.threejs.org/t/how-to-unit-test-three-js/57736/2 "2023-11-03T21:57:02Z")

</div>

Assuming you mean normal JavaScript unit tests, where you can check whether some variable is in state A or state B… personally I like [Ava](https://github.com/avajs/ava) or [Tape](https://github.com/ljharb/tape). For me Jest is too complicated, and not worth it unless you’re testing frontend components. If it’s important that tests run in a web browser, there’s [Tape Run](https://www.npmjs.com/package/tape-run), but most of the newer test frameworks aren’t very good at this, and going back to something older like [MochaJS](https://mochajs.org/) might make sense.

Example with Tape here:

> <https://github.com/donmccurdy/three-pathfinding/blob/96bba1de83a49ec2b4296cca38286fd6ef3783b8/tests/index.test.js>

If you need to instantiate renderers, controls, or other things that rely on the DOM, it gets a bit more complicated and you may want additional tools like [jsdom](https://github.com/jsdom/jsdom) (to mock the browser environment in Node.js) or [sinon](https://sinonjs.org/) (to stub out methods on objects).

I’d try to avoid running actual WebGL in tests, unless it’s absolutely essential to what you’re testing. Tools like [Cypress](https://docs.cypress.io/guides/overview/why-cypress) can help with screenshot tests, but require more work than traditional unit tests.

---

<div class="post-metadata">

**Author:** ![michealmyers81](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/michealmyers81/32/35656_2.png) [@michealmyers81](https://discourse.threejs.org/u/michealmyers81)\
**Post date:** [November 4, 2023, 3:34am UTC](https://discourse.threejs.org/t/how-to-unit-test-three-js/57736/3 "2023-11-04T03:34:38Z")

</div>

Thanks, I’ll give it a try
