Well, my little practice app has a few features now. In fact, it is already getting a bit out of hand. If I'm going to add more to it, I need to treat the project with a little more respect. So, carrying on from my last node.js post about a simple module, I'm going to add a unit test. Then, I begin the journey of placing most of my controller logic in modules (carefully) and unit testing everything. Then, I can start to develop with much more confidence.
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.
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.
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:
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:
Looks good. Now I'm ready to start breaking this thing up and adding more tests.