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

23 thoughts on “Unit Testing using Karma and Grunt

  1. Of class, what a breathtaking world wide web and illuminating content articles, I completely will bookmark your web-site.Least difficult Regards!. Excellent articles or blog posts & Pleasant a web site. A rise in just Additional Even further.

  2. I think other site proprietors should take this web site as an model, very clean and great user genial style and design, let alone the content. You’re an expert in this topic!

  3. It’s truly a nice and helpful piece of information. I’m satisfied that you simply shared this helpful information with us. Please keep us up to date like this. Thank you for sharing.

  4. I have not checked in here for some time as I thought it was getting boring, but the last few posts are good quality so I guess I will add you back to my everyday bloglist. You deserve it my friend :)

  5. Hi, I think your blog might be having browser compatibility issues.
    When I look at your blog in Opera, it looks fine but when opening in Internet Explorer, it
    has some overlapping. I just wanted to give you a quick heads up!
    Other then that, excellent blog!

  6. excellent submit, very informative. I’m wondering why the other specialists of this sector do not understand this. You should proceed your writing. I am sure, you have a huge readers’ base already!

  7. you are actually a good webmaster. The web site loading pace is incredible. It kind of feels that you’re doing any unique trick. Moreover, The contents are masterwork. you’ve performed a excellent activity in this subject!

  8. Wow! This could be one particular of the most beneficial blogs We’ve ever arrive across on this subject. Actually Fantastic. I’m also an expert in this topic so I can understand your effort.

  9. I read a lot of interesting content here. Probably you spend a lot of
    time writing, i know how to save you a lot of time, there is an online tool that creates high quality, SEO friendly posts
    in minutes, just type in google – laranitas free content

Add Comment Register



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="">