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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s