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