In order to better understand testing in Node I decided to work out the Bowling Game kata from scratch. The kata is an exercize to pratice the disciplines of good TDD. But is also a good starting point for learning a new environment.
The first step was to figure out how testing was done in NodeJS. After googling around I found a good starting point by Matthew Palmer. This example uses Mocha and Expect.js.
The next step was to figure out how to get a project initialized with Node. More searching revelaed that I could just use
node init <my project>
but I wouldn't do it that way again. All I got was some annoying prompts that generated a package.json. Next time I would just generate this file by hand.
After doing some more reading about how mocha works, and with some experience using Chai the previous day, I decided to use Chai instead of Expect.js.
My final package.json looks like this.
{
"name": "bowling-game-js",
"version": "0.0.0",
"description": "Bowling Game Kata in JS with Node",
"main": "index.js",
"scripts": {
"test": "mocha test"
},
"author": "Matthew Thorley",
"license": "BSD"
,"devDependencies": {
"mocha": "*",
"chai": "*"
}
}
Note that in addition to adding the devDependencies for Mocha, and Chai, I also added the test command "mocha test" to scripts:test.
With that in place I could write my first test. I read some of the Mocha best practices and looked at a few example projects to see how things should be setup. Then I created a test directory, and my first test, test/bowlinggame.js. In other testing frameworks I've used in tht past test files usually were named testsomething or something_test. But the common practice in this ecosystem seems favor omitting the word 'test' from filenames.
mkdir test
vim test/bowling_game.js
Here's my first test.
var chai = require('chai')
chai.should()
var BowlingGame = require ('../bowling_game').BowlingGame
describe('new BowlingGame()', function() {
it('should create a new BolwingGame', function() {
game = new BowlingGame
game.should.be.a('Object')
})
})
This test fails because there is no bowlinggame file or BowlingGame object. Getting it to pass is easy to do. Just add a bowlinggame.js file to the root of the project and export a BowlingGame object in it.
vim bowling_game.js
Content of bowling_game.js
exports.BowlingGame = function() {
}
When I run 'mocha tests' the tests pass.
Happy Hacking, MT