mocha is a feature-rich JavaScript test framework running on node and the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on GitHub.
Add mocha and should.js to your package.json file as devDependencies
.
"devDependencies": {
"mocha": "*",
"should": ">= 0.0.1"
}
run npm install
to update
Create a mocha.opts
file to store common config settings.
test/mocha.opts
--require should
--reporter spec
--ui bdd
--recursive
For more detailed information on mocha.opts please read the mocha documentation
Add a test command to your packages.json
file. This will allow you to run test by typing npm test
in the console.
"scripts": {
"start": "node_modules/.bin/supervisor app",
"test": "node_modules/.bin/mocha -w"
},
The test command will start a test runner in the background. Any time a file is changed it will run the test suite. For more information on npm scripts please read npm scripts
Create a test case.
test/addition.js
'use strict';
describe('addition', function () {
it('should add 1+1 correctly', function (done) {
var onePlusOne = 1 + 1;
onePlusOne.should.equal(2);
// must call done() so that mocha know that we are... done.
// Useful for async tests.
done();
});
});
Open a new terminal window
Run mocha
test runner
npm test
You should see:
addition
✓ should add 1+1 correctly
✔ 1 test complete (3ms)
Keep the terminal window open. We want mocha to continue to run tests