Skip to content
Aaron Bell edited this page May 14, 2014 · 11 revisions

This article shows how to get three.js into a node.js environment so that you can execute automated tests. Tests can be run on the command line, or by automated CI tools like Travis.

The short version

If you're comfortable with node and npm,

  1. $ npm install three --save-dev, and
  2. Add var THREE = require('three'); to your test.

Create a testable project from scratch

If you're not familiar with these tools, here's a quick guide.

Basic setup

  1. Install npm and nodejs. The shortest path typically looks something like

    $ sudo apt-get install -y npm nodejs-legacy
    # fix any problems with SSL in the default registry URL
    $ npm config set registry http://registry.npmjs.org/
    
  2. Make a new project directory, $ mkdir test-example; cd test-example

  3. Ask npm to create a new project file for you: $ npm init and accept all defaults by hitting Enter on all the prompts. This will create package.json.

  4. Try and start the test feature with $ npm test. This will fail, which is expected. If you look in the package.json, the definition of the test script is "test": "echo \"Error: no test specified\" && exit 1"

Add mocha

We're going to use mocha.

  1. Install mocha with $ npm install mocha --save-dev. Notice that node_modules/ is created and your dependencies appear in there. Also notice that your package.json has been updated: the property devDependencies is added and updated by the use of --save-dev.

  2. Edit package.json to use mocha for testing. When test is invoked, we just want to run mocha and specify a verbose reporter. By default this will run anything in test/.

    "test": "mocha --reporter list"
    
  3. Rerun the test with $ npm test. This should now succeed, reporting 0 passing (1ms) or similar.

Clone this wiki locally