1.Install nodejs latest version(v8.7)
2.Install mocha
3.Create app js and test js files.
//calc.js to be tested by mocha
module.exports = function (...rest) {
var sum = 0;
for (let n of rest) {
sum += n;
}
return sum;
};
//calc-test.js
const assert = require('assert');
const sum = require('./calc');
describe('#calc.js', () => {
describe('#sum()', () => {
it('sum() should return 0', () => {
assert.strictEqual(sum(), 0);
});
it('sum(1) should return 1', () => {
assert.strictEqual(sum(1), 1);
});
it('sum(1, 2) should return 3', () => {
assert.strictEqual(sum(1, 2), 3);
});
it('sum(1, 2, 3) should return 6', () => {
assert.strictEqual(sum(1, 2, 3), 6);
});
});
});
4.Run the test.
网友评论