Skip to main content
Jackson Bates

Setting up a simple testing suite

You may want to have your own simple test runner to ensure your functions return accurate results.

I used Mocha and Chai. Here's some sample code and a walk through for using it. Note: the challenge I wrote these tests for was return the sum of all multiples of 3 or 5 that are below a number N, where N is an input parameter to the function.

var should = require("chai").should();
var basic = require("./multJacksonBates");

describe("basic", function () {
it("should return 23 when passed 10", function () {
basic(10).should.equal(23);
});
it("should return 78 when passed 20", function () {
basic(20).should.equal(78);
});
it("should return 2318 when passed 100", function () {
basic(100).should.equal(2318);
});
it("should return 23331668 when passed 10000", function () {
basic(10000).should.equal(23331668);
});
it("should return 486804150 when passed 45678", function () {
basic(45678).should.equal(486804150);
});
});

Walk through #

Pre-req: Install Node & NPM, you can open and type in your OS's terminal / command line

// multJacksonBates.js
function multJacksonBates(n) {
return 23;
}

module.exports = multJacksonBates;

Additional resources #

If you decide to write several functions and want to test them for efficiency, you can modify the benchmark script from here: Benchmark

You can find more information about Mocha and Chai at the following links: