code everywhere

JavaScript lightweight testing framework

posted on August 23, 2015, 4:51 pm in js, nodejs

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(descriptionfn) {

        if (
verbose)
            
console.log("\nDescribe: " description);

        var 
it = function(doesfn) {
            var 
_this this;
            
this.testsRunning += ("" fn).match(/expect\(/gi).length;

            if (
verbose)
                
console.log("\tIt: " does);

            var 
expect = function(equals) {
                return ({
                    
toEqualtoEqual,
                    
toBetoEqual,
                    
toFailtoFail
                
});

                function 
toEqual(expected) {
                    var 
pass = (expected == equals) ? "PASS" "FAIL";
                    var 
color = (expected == equals) ? 1;

                    if (
verbose)
                        
console.log("  \033[3" color "m" pass "\033[39m\t\tTest: (expected, equals) => (" +
                            
expected ", " equals ")");

                    
testResults.push({
                        
describedescription,
                        
itdoes,
                        
expectedexpected,
                        
equalsequals,
                        
resultexpected == 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({
                        
describedescription,
                        
itdoes,
                        
expected"-",
                        
equals"... " because,
                        
resultfalse
                    
});

                    
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(ab) {
        return 
b;
    };
    
this.sub = function(ab) {
        return 
b;
    };
    
this.mul = function(ab) {
        return 
b;
    };
}

// Tests ...
var calc = new calc();

describe("My calculator classs", function(it) {

    
it("should add 2 numbers", function(expect) {
        
expect(calc.add(55)).toEqual(10);
        
expect(calc.add(24)).toEqual(6);
    });

    
it("should subtract 2 numbers", function(expect) {
        
expect(calc.sub(55)).toEqual(0);
        
expect(calc.sub(24)).toEqual(-2);
    });

    
it("should multiply 2 numbers", function(expect) {
        
expect(calc.mul(55)).toEqual(25);
        
expect(calc.mul(24)).toEqual(8);
    });

    
it("should divide 2 numbers (Fail Example)", function(expect) {
        
expect(calc.mul(55)).toEqual(99);
        
expect(calc.mul(24)).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(fn5000);
    });
});

// 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

recent posts

< back
find something helpful? send us some coin BTC 19u6CWTxH4cH9V2yTfAemA7gmmh7rANgiv

(ad) Need Hosting? Try Linode, VPS Starting from $5

privacy policy