nodeunit for unit testing in node.js

The following unit test example with nodeunit testing framework is based on nodeunit versions 0.10.2, and it forks off from this original tutorial, which is a bit outdated by the time of this post. And because of that, you might getting error like “TypeError: Cannot read property ‘setUp’ of undefined

1. Install nodeunit from your command line window.

npm install nodeunit -g

2. Create a file /your_project_root_folder/modules/doubled.js with the following code. If you don’t have the modules folder already, create it.

exports.calculate = function (num) {
    if (typeof num === 'number') {
         return num * 2;
    }
    else {
        throw new Error('Expected a number');
    }
};
exports.read = function () {
    var stdin = process.openStdin();
    
    stdin.on('data', function (chunk) {
        var num = parseFloat(chunk);
        try {
            var result = exports.calculate(num);
            console.log('doubled: ' + result);
        }
        catch (e) {
            console.log(e);
        }
        process.exit();
    });
};

3. Create a test file /your_project_root_folder/test/test_doubled.js with the following code. If you don’t have the test folder, create it. This test folder is a default folder where the nodeunit will look for when you run the tests.

var doubled = require('../modules/doubled'),
	events = require('events');
module.exports = {
    setUp: function (cb) {
        this._openStdin = process.openStdin;
        this._log = console.log;
        this._calculate = doubled.calculate;
        this._exit = process.exit;

        var ev = this.ev = new events.EventEmitter();
        process.openStdin = function () { return ev; };
        return cb();
    },
    tearDown: function (cb) {
        // reset all the overidden functions:
        process.openStdin = this._openStdin;
        process.exit = this._exit;
        doubled.calculate = this._calculate;
        console.log = this._log;
        return cb();
    },
	'test calculate': function(test) {
		test.equal(doubled.calculate(2), 4);
	    test.equal(doubled.calculate(5), 10);
	    test.throws(function () { doubled.calculate(); });
	    test.throws(function () { doubled.calculate(null); });
	    test.throws(function () { doubled.calculate(true); });
	    test.throws(function () { doubled.calculate([]); });
	    test.throws(function () { doubled.calculate({}); });
	    test.throws(function () { doubled.calculate('asdf'); });
	    test.throws(function () { doubled.calculate('123'); });
	    test.done();
	},
    'a value other than a number': function (test) {
	    test.expect(1);
	    process.exit = test.done;
	    doubled.calculate = function () {
	        throw new Error('Expected a number');
	    };
	    console.log = function (str) {
	        test.equal(str, 'Error: Expected a number');
	    };
	    doubled.read();
	    this.ev.emit('data', 'asdf');
    },
    'a number': function (test) {
        test.expect(1);

        process.exit = test.done;
        console.log = function (str) {
            test.equal(str, 'doubled: 24');
        };
        doubled.read();
        this.ev.emit('data', '12');
    }
};

4. Run the test command in your project root folder.

nodeunit test

5. You should see the following test results.

test_doubled
✔ test calculate
✔ a value other than a number
✔ a number

OK: 11 assertions (11ms)

Search within Codexpedia

Custom Search

Search the entire web

Custom Search