Mocha is a feature-rich JavaScript test framework running on Node.js and in 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.
Some of the features of mocha are as follow:
- simple async support, including promises.
async test timeout support.
before, after, before each, after each hooks (very useful to clean the environment where each test!).
test coverage reporting.
1)Installing mocha with npm globally in terminal
npm install –global mocha
2)Making directory
mkdir test
3)Knowing version of mocha
which mocha
4)Creating package.json file
“scripts”: {
“test”: “mocha”
}
5)Run test with;
npm test
6)Creating addition.js file in test folder
‘use strict’;
var assert=require(“assert”);
describe(‘addition’, function () {
it(‘should add 5+6 correctly’, function (done) {
var a = 5 + 6;
assert(a===11);
done();
});
});
7)Running addition.js in terminal
mocha test/addition.js //test folder
8)Using reporter in browser
npm install –save-dev mochawesome
mocha test/addition.js –reporter mochawesome
9)Using different reporter
mocha test/addition.js –reporter progress
mocha test/addition.js –reporter nyan