Install Mocha JS globally npm install -g mocha
Then run mocha init test
in the root of your project.
"test" is the directory where the testing files will be created, and it's where mocha will look to run tests from.
Then run tests with mocha
.
Sorted.
The previous approach can work in the browser but requires some fixing.
Because test/tests.js loads an assertion library via require (which is only available in Node), you'll get an error. You need to load an assertion library into the page manually.
A better way to setup running tests in the browser is explained in this guide: https://gist.github.com/maicki/7781943
It allows you to run tests in the browser (for real) and inside of a headless browser with Phantom JS, with the help of Grunt JS. Tests are now part of your build process.
If you want to run tests with mocha directly, add the following to the top of test/tests.js (assuming that you're using Chai):
if(typeof require !== 'undefined'){
var chai = require('chai');
}
var assert = chai.assert;