This article shows how to get three.js into a [link:https://nodejs.org/en/ node.js] environment so that you can execute automated tests. Tests can be run on the command line, or by automated CI tools like [link:https://travis-ci.org/ Travis].
If you're comfortable with node and npm,
$ npm install three --save-dev
and add
const THREE = require('three');
to your test.
If you're not familiar with these tools, here's a quick guide (for linux, the installation process will be slightly different using windows, but the NPM commands are identical).
$ 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/
$ mkdir test-example; cd test-example
$ npm init
and accept all defaults by hitting Enter on all the prompts.
This will create package.json.
$ 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"
$ 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.
"test": "mocha --reporter list"
$ npm test
This should now succeed, reporting 0 passing (1ms)
or similar.
$ npm install three --save-dev
$ npm show three versions
to see
what's available. To tell npm the right one, use
$ npm install three@0.84.0 --save
(0.84.0 in this example). --save makes this a dependency of this project, rather than
dev dependency. See the docs [link:https://www.npmjs.org/doc/json.html here] for more info.
$ mkdir test
const THREE = require('three');
const assert = require('assert');
describe('The THREE object', function() {
it('should have a defined BasicShadowMap constant', function() {
assert.notEqual('undefined', THREE.BasicShadowMap);
}),
it('should be able to construct a Vector3 with default of x=0', function() {
const vec3 = new THREE.Vector3();
assert.equal(0, vec3.x);
})
})
The THREE object should have a defined BasicShadowMap constant: 0ms
The THREE object should be able to construct a Vector3 with default of x=0: 0ms
2 passing (8ms)
Items 2 and 3 will vary depending on how you manage your code. In the example of Physics.js given above, the export part is right at the end. We assign an object to module.exports:
//=============================================================================
// make available in nodejs
//=============================================================================
if (typeof exports !== 'undefined')
{
module.exports = Physics;
}
If you're already using something clever like require.js or browserify, skip this part.
Typically a three.js project is going to run in the browser. Module loading is hence done by the browser executing a bunch of script tags. Your individual files don't have to worry about dependencies. In a nodejs context however, there is no index.html binding everything together, so you have to be explicit.
If you're exporting a module that depends on other files, you're going to have to tell node to load them. Here is one approach:
//=============================================================================
// setup for server-side testing
//=============================================================================
if (typeof require === 'function') // test for nodejs environment
{
const THREE = require('three');
const MY3 = require('./MY3.js');
}