There seems to be a few unit testing frameworks out there for node.js. So far I've checked out Expresso and nodeunit as well has having a glance at vows for a more BDD approach. For this project, I'm choosing nodeunit because it seems like it will do the job and I like the syntax a little more than Expresso.
Nodeunit Install
Like most every package, nodeunit installs very smoothly with npm.
npm install nodeunit -g
In my environment (ubuntu), nodeunit needed to be globally installed (-g) in order for the nodeunit command to be available on the command line.
First Test
The first test I'm going to write is going to make sure that my format method in the dateFormatter.js module is working as expected. So I first created a directory for tests (mkdir tests), then I added a new file named dateFormatterTest.js into the new directory. Here is the code for the test module:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var target = require('../modules/dateFormatter'); | |
exports.dateShouldPrintCorrectly = function(test){ | |
// given | |
var date = new Date('25 Dec, 1995 23:15:00'); | |
// when | |
var result = target.format(date); | |
// then | |
test.equal('25/12/1995 11:15 PM', result); | |
test.done(); | |
}; |
I've got my /tests directory on the same level as my /modules directory so I just go up a directory to require the module I want to test.
First Test Run
So, I'm ready for my first test run. nodeunit will run all the tests in a directory so I can just type nodeunit /tests in the root directory to run the test. And here is the result:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dateFormatterTest | |
✔ dateShouldPrintCorrectly | |
OK: 1 assertions (9ms) | |
jumble@jumble-Inspiron-600m:~/Documents/Dev/Node/Projects/clog/src$ |
Looks good. Now I'm ready to start breaking this thing up and adding more tests.
No comments:
Post a Comment