Sunday, 14 April 2013

A quick peek at nodeunit

I've been hacking around with node for a while now (as many people have), but because much of it has been just that, hacking, and very little of the code base grows beyond a couple of hundred lines of code, I've not been writing unit tests. tisk tisk.

I'm not really planning anything big in the near future, but if I embark on building something that is more than a prototype or a hack I'm going have to unit test. So tonight I'm going to have a quick poke around to see what folks are using for unit testing node application.

I'm not sure what the de facto framework is, but I'm going to follow the pattern set over the years of _something_Unit. So, I'm going to punt at there being a NodeUnit. Google "nodeunit" -> Bang!

There is already a good article on getting started, so I'm not going to add any value here tbh. But I'll carry on and record my concise notes, just in case I need to refer to them myself in the future.

I'm on Ubuntu, though it shouldn't matter which platform you're on. (if truth be known, I've had some issues with code being cross-platform compatible with node)

$ sudo npm install nodeunit -g

I thought I'd knock up something that is at least vaguely security related. I'm not getting into arguments around random passwords vs memorable passwords, nor am I starting on hashing, salting or key stretching; this is simply something that allows me to test drive nodeunit quickly and simply.

The example is a simple, old-skool password policy. Stuff like:
  • Minimum length
  • Upper case
  • Lower case
  • Numeric
  • Special characters
I'm keeping it simple. Ensure the above criteria are validated. So, here's the code. (there's nothing clever here)

The tests are simple too. I want to ensure the policy is enforced and that nodeunit can handle the tests I want to exercise.
  • If minimum length is not met, throw an error
  • If it is not a string, throw an error
  • If the criteria is not met, fail the test
I added the random generation of test passwords - I thought about using Markov Chains to generate the passwords, but then thought, that's a good subject for a different post.

As an aside, I kinda like the fact that the validator is validating the test utility that generates passwords to be used in the tests, code validating the validation tests...

Anyhoo; running the tests...

$ nodeunit test_validator.js

The code ran first time, surprising since I wasn't using an IDE, it was the old fave TextPad. More surprising though was how quickly it ran; I upped the random generating of passwords to 5,000,000 and it took only 15 seconds to execute. Cool.

So, to summarise my fleeting introduction to nodeunit.
  • It's fast
  • Easy to install
  • Quick to install
  • Easy to understand
  • Fast
I'll kick the tyres of nodeunit a little more in the future should I get more serious with node, and if there are any notable observations I'll add them as a comment to this post.

There are mode frameworks out there too, but from experience, you tend to stick with what works until it doesn't work. So, for me, I think nodeunit will be my de facto node...unit.