JavaScript lightweight testing framework
posted on August 23, 2015, 4:51 pm in
A small and light testing framework for any JavaScript app. It follows the standard describe(), it(), expect() notation. Uses NO external dependencies, so you can get up and running quickly.
The Code
/*
* testing.js (Aug 23 2015)
* Copyright 2015, http://codeeverywhere.ca
* Licensed under the MIT license.
*/
module.exports = function() {
this.testsRunning = 0;
this.testsPass = 0;
this.testsFail = 0;
var verbose = true; // Use console.log output
var testResults = []; // Results Array
var startTime = new Date().getTime(); // Start time in ms
var endTime = -1; // Runtime of tests in ms
this.testTime = endTime;
this.printTestResults = function() {
console.log("\n*** Tests Completed, Results: " + testsRunning + " tests run, \033[32m" + testsPass +
" PASS, \033[31m" + testsFail + " FAIL\033[39m in " + (endTime / 1000) + "s");
};
this.describe = function(description, fn) {
if (verbose)
console.log("\nDescribe: " + description);
var it = function(does, fn) {
var _this = this;
this.testsRunning += ("" + fn).match(/expect\(/gi).length;
if (verbose)
console.log("\tIt: " + does);
var expect = function(equals) {
return ({
toEqual: toEqual,
toBe: toEqual,
toFail: toFail
});
function toEqual(expected) {
var pass = (expected == equals) ? "PASS" : "FAIL";
var color = (expected == equals) ? 2 : 1;
if (verbose)
console.log(" \033[3" + color + "m" + pass + "\033[39m\t\tTest: (expected, equals) => (" +
expected + ", " + equals + ")");
testResults.push({
describe: description,
it: does,
expected: expected,
equals: equals,
result: expected == equals
});
if (expected == equals)
testsPass++;
else
testsFail++;
endTime = (new Date().getTime()) - startTime;
return this;
}
function toFail(because) {
if (verbose)
console.log(" Test: ", "Result:", false, " ... " + because);
testResults.push({
describe: description,
it: does,
expected: "-",
equals: "... " + because,
result: false
});
testsFail++;
endTime = (new Date().getTime()) - startTime;
return this;
}
};
fn(expect);
};
fn(it);
};
};
Save this code as testing.js and add it to your node.js script using require('./testing')().
example usage
Simple example, testing using our calc app.
require('./testing')();
// Test function
function calc() {
this.add = function(a, b) {
return a + b;
};
this.sub = function(a, b) {
return a - b;
};
this.mul = function(a, b) {
return a * b;
};
}
// Tests ...
var calc = new calc();
describe("My calculator classs", function(it) {
it("should add 2 numbers", function(expect) {
expect(calc.add(5, 5)).toEqual(10);
expect(calc.add(2, 4)).toEqual(6);
});
it("should subtract 2 numbers", function(expect) {
expect(calc.sub(5, 5)).toEqual(0);
expect(calc.sub(2, 4)).toEqual(-2);
});
it("should multiply 2 numbers", function(expect) {
expect(calc.mul(5, 5)).toEqual(25);
expect(calc.mul(2, 4)).toEqual(8);
});
it("should divide 2 numbers (Fail Example)", function(expect) {
expect(calc.mul(5, 5)).toEqual(99);
expect(calc.mul(2, 4)).toEqual(99);
});
});
printTestResults();
and an example with a delayed response:
// Tests with delay ...
describe("A test with a delayed response", function(it) {
it("should complete after 5 sec", function(expect) {
var fn = function() {
expect(5).toEqual(5);
};
setTimeout(fn, 5000);
});
});
// Check timer ...
var timer = setInterval(function() {
if (testsRunning == testsPass + testsFail) {
clearInterval(timer);
printTestResults();
}
}, 500);
and sample terminal output:
bash -- 70x32
Describe: My calculator class It: should add 2 numbers PASS Test: (expected, equals) => (10, 10) PASS Test: (expected, equals) => (6, 6) It: should subtract 2 numbers PASS Test: (expected, equals) => (0, 0) PASS Test: (expected, equals) => (-2, -2) It: should multiply 2 numbers PASS Test: (expected, equals) => (25, 25) PASS Test: (expected, equals) => (8, 8) It: should divide 2 numbers (Fail Example) FAIL Test: (expected, equals) => (99, 25) FAIL Test: (expected, equals) => (99, 8) *** Tests Completed, Results: 8 tests run, 6 PASS, 2 FAIL in 0.023s Describe: A test with a delayed response It: should complete after 5 sec PASS Test: (expected, equals) => (5, 5) *** Tests Completed, Results: 9 tests run, 7 PASS, 2 FAIL in 5.027s