Creating a test in vtk.js

This guide illustrates how to add tests to the vtk.js repository and how to run and debug them.

First each VTK class can have several tests spread among several files but we also have infrastructure for global tests which live inside Sources/Testing/test*.js.

Class tests

In order to add test to vtk.js you will need to create a test directory underneath your class directory.

That test directory should contain as many test file as you like with their associated baselines if any. By convention we expect each test file to be prefixed with test similar to testDataArray.js.

Testing plain JavaScript

Some vtkClass’es don’t necessarily involve rendering and can be tested without a WebGL environment. In which case a plain JavaScript test could be written as follows and an additional test() function could be added within the same file:

Registering a test

As opposed to examples and api documentation, tests won’t be picked up automatically and it is the responsability of the author to register it on the test suite.This may change as the project evolves.

To add a test to the suite, you need to import it inside Sources/tests.js.

Running all the tests

In order to run all the registered test, run the following command:

$ npm run test

That command can also be written as npm t or npm test however it will run all tests.

Running a single test for debugging

Since we have single entry points for all the tests we run, you can comment out every file except the one you want to run inside Sources/tests.js.

Then executing npm t will only run that file, but if you want to open a browser and debug the actual code, you can do it with the following command:

$ npm run test:debug

This will automatically open a browser and run the test. But you can manually open http://localhost:9876/debug.html with any browser and start debugging.

Moreover, when doing some rendering that’s a great way for building your baseline image.

Creating a new baseline image

Follow the following procedure to create a new baseline or change an existing baseline for a test.

Add an invalid baseline (any PNG file) and rename it as the required baseline.For example, to create a baseline for testCylinder.js copy testCone.png to Sources/Filters/Sources/CylinderSource/test/testCylinder.png.