Mocha - a JavaScript test framework for Node.js programs, featuring browser support, asynchronous testing, test coverage reports, and use of any assertion library.
Chai - BDD (behavior driven development) / TDD (test driven development) assertion library for node and the browser that can be delightfully paired with any javascript testing framework.
-
Makes sure you have a intialized NodeJS environment.
$ npm init
-
Install testing dependencies
$ npm install mocha chai --save-dev
-
Create a directory to house your test. Below is an example of testing an application
root/ ├─── app/ │ └── app.js ├─── test/ │ ├── appTest.js └── index.js
// app.js
module.exports = function () {
return 'hello';
}
// appTest.js
const assert = require('chai').assert;
const app = require('../app')
describe('App', function () {
it('app should return hello', function () {
assert.equal(app(), 'hello');
});
it('should return type string', function () {
assert.typeOf(app(), 'string');
})
});