Automated testing with Grunt and Jasmine

On my last post Conway’s Game of Life I’ve used jasmine for testing my code.

Now, I would like to show how to automate the process of running all the tests.  and for this I will be using Grunt (v0.4.5).

This will be more like a recipe because there are already several good tutorials about grunt and jasmine, so if you’re new to Grunt or Jasmine, I advise you to get some basic reading first.

So let’s do this!

1. Because Grunt is built on Node, we have to install node and npm.

2. Open the command line.

3. install Grunt command line by running the following command:

npm install grunt-cli -g

-g will make it global

Note: on version 0.4 grunt was separated in grunt command line and grunt engine. Grunt command line typically is installed globally and Grunt engine locally.

4. Go to the project directory.

5. Install Grunt engine:

npm install grunt

6. Create a package.json. This can be done by hand or automatically using the command:

npm init

This will ask you a few questions and by the end it will create the file.

7. Install jasmine plugin for grunt:

npm install grunt-contrib-jasmine --save-dev

The optional flag –save-dev will save our dependencies on our file package.json

Note: This command will also install phantom.js which is how it runs the tests

8. Create a gruntfile.js with the configuration needed

 module.exports = function(grunt) {
     // jasmine configuration - indicates the source files, the specs files and vendor files
     jasmine: {
       pivotal: {
         src : 'lib/js/*.js',
         options: {
           specs : 'spec/GameSpec.js',
           vendor: [

 // load plugins

 // Test task
 grunt.registerTask('test', ["jasmine"]);


9. Run the tests:

grunt test

And you can check that all tests are passing:

Webucator, a provider of JavaScript training, produced a nice little video inspired on this post. You can watch, step by step, how easy is to test javascript with Grunt and Jasmine.

HTML5 Conway’s Game of Life

I have created a HTML5 version of the Conway’s Game of Life.

The “game” is a zero-player game, meaning that its evolution is determined by its initial configuration and does not require any other input. The “game” is based in a grid with cells. Each cell has only 2 states: alive and dead. There are only 4 rules that determines how the game evolves:

  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overcrowding.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

You can try it here.

I used javascript, knockout and Jasmine and the source code is available on my Github.