When I’m first learning a language, one of the first projects I tackle is Bob Martin’s Bowling Game kata. I’ve written about code katas before, suffice it to say I still think they are a valuable learning tool. When learning a new language, one of the first questions I want to answer is how to unit test my code, and this kata presents a fairly simple problem that’s well-suited to test-first design.
So, I coded up a solution to the kata using node.js. There are plenty of examples on the web already, but I didn’t use them directly. I fumbled around with node.js for a while, then I went looked to improve my knowledge of the language’s facilities and constructs.
I used Mocha as my test framework. I discovered after a while that Mocha has a mode where you can have Mocha watch a folder for changes and execute tests using
mocha -w. I find the instant feedback of continuous testing to be very valuable.
I started with
assert for testing, but ended up choosing should, because I prefer BDD-style syntax when making assertions. Here’s a representative sample, taken from early on in the kata:
With assert, the assertion would look like:
For simple assertions, assert is legible. However, for more complicated ones, I find that BDD-style syntax more naturally expresses what I’m verbalizing as I construct the test.
The code to actually perform score the game is (game.js):
I did have some trouble understanding how modules expose methods and variables until I read How to Use Exports in NodeJS, which I found concise and informative.
After I got this kata under my belt, I started doing more research and getting interested in the MEAN stack (MongoDB, Express, AngularJS, and Node.js). Next time, I’ll show the kata with the inclusion of Express.