Jasmine, Grunt, and jasmine_node: testing via a build task

October 19, 2013

So continuing on from my last post using Twit with node js. This post covers the work need to get jasmine set up to test the server application.

Wanting to take on the workflow of TDD (test driven development)  I decided to use jasmine as my test framework, for this I used jasmine-node, this is a node package configured for node projects. Jamsie node can be installed using the following command:

npm install jasmine-node

Once installed test files can be created, to get started I created a few simple tests that would make requests to the application and rest the responses, shown below:

These two tests use the node module “request” this makes a http request then the result is inspected, once this file has been created it can be run using the following command.

jasmine-node –verbose –captureExceptions server/test/spec/

The command is calling the installed tool jasmine-node then using the –verbose option to show all details printed by the tests and any errors, captureExecptions will show any errors and stack traces which fail before the “expect” function in the code this will show the error if the test fails before the expect.

Once jasmine was installed and set up to run tests I wanted to add this to the build process so I decided to install grunt, using the following command.

npm install grunt

Once grunt is installed I then needed to create a Gruntfile.js file, this will hold all the configurations and grunt commands that can be used for the project. As I want to use grunt to run jasmine I looked and found that there is grunt plugin for jasmine-node, I then installed this using the following command:

npm install grunt-jasmine-node –save-dev

This will install the grunt jasmine-node plugin and also add this to the dev dependencies in the package.json once installed the next process is to created and config the grunt file shown below:

The gruntfile starts with the module definition then the grunt commands that can be used for this case the only command is the jasmine-node command. Once set up I was able to run the test via the following command:

grunt jasmine_node

This will come in handle once the project is running on a CI (continuous integration) server as it will allow the tests to be run as part of the build task.


Tags: , , ,