JavaScript Unit Testing


One day you will switch to some other programming language; one of your first tasks will be to figure out its unit-testing framework. ~How to Design Programs


index.js with a simple function to test. We also export the function.

// provide the function
exports.add1 = add1;

// Number -> Number
// adds 1 to x
function add1(x) {
  return (x + 1);
}

test/test.js which imports the function and tests it using the MochaJS library.

// for testing
var assert = require('assert');

// importing the functions for testing.
let index = require('../index.js');

// TESTS
describe('Tests for add1', () => {
    it('for 0', () => { assert.equal(index.add1(0), 1); });
    it('for 1', () => { assert.equal(index.add1(1), 2); });

});