Unit Testing using Karma and Grunt

In my previous tutorial, “Running Jasmine Unit Test cases using Karma”, I have shown you how to configure Karma and running test cases using it.

In this tutorial, I will show you how to install, configure Grunt, the JavaScript task runner and running our existing test cases using it.

We need to follow the below mentioned steps:

You can skip step 4 and 5 as we have already installed and configured Karma.

1. Create Gruntfile.js

module.exports = function (grunt) {
grunt.initConfig({

});
};

2. Create package.json

{
“name”: “javascript-unit-testing”,
“description”: “How to unit test your JavaScript codes”,
“version”: “0.0.1″,
“dependencies”: {
“grunt”: “~0.4.2″
},
“devDependencies”: {
“grunt-cli”: “~0.1.13″,
“karma”: “~0.10″,
“grunt-karma”: “~0.8.2″
}
}

3. Go to the project root folder and run:
npm install
4. npm install -g karma-cli
5. karma init karma.conf.js

Specify all the required details

6. Once that’s done, add this line to your project’s Gruntfile

grunt.loadNpmTasks(‘grunt-karma’);

7. Add this in grunt.initConfig of Gruntfile.js:

karma: {
unit: {
configFile: ‘karma.config.js’
}
}

8. Go to your project folder from command line and type:

> grunt karma

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">