Testing With AngularJS Part 2: Other Useful Karma Plugins

  • In this post, I will talk about how you can use three additional plugins to extend Karma and enhance your testing.
  • For unit testing directives that use the templateUrl setting or for unit testing routes, you can install an additional Karma plugin to convert HTML files to JS files.
  • Now you can include HTML files as modules in unit test specs and no errors should be thrown.
  • You can install a Karma plugin to run JSHint as part of your testing process.
  • Karma has several useful plugins that can enhance your testing process.

This is the second part of my series describing how I have learned to more easily test my AngularJS projects. In part one, Setting up Unit Testing With Karma, we configured Karma to run our unit tests. In this post, I will talk about how you can use three additional plugins to extend Karma and enhance your testing.

@KingsleyTagbo: How To Test AngularJS Using Karma Plugins RT @mariaknabe #karma #testing #coverage #unittest

This is the second part of my series describing how I have learned to more easily test my AngularJS projects. In part one, Setting up Unit Testing With Karma, we configured Karma to run our unit tests. In this post, I will talk about how you can use three additional plugins to extend Karma and enhance your testing.

Be sure to check out the example app for a full, working implementation.

For unit testing directives that use the templateUrl setting or for unit testing routes, you can install an additional Karma plugin to convert HTML files to JS files. Otherwise, running the tests might throw this error: “Unexpected request: GET partials/directive.html No more request expected”.

Install the plugin with Node.

Now you can include HTML files as modules in unit test specs and no errors should be thrown.

Test coverage is a measurement that describes how much of you code has been tested. The karma-coverage plugin will report this so you know how much of your app is tested with your unit tests.

Install the coverage plugin with Node.

Now run the karma start test/karma-conf.js –single-run command. You should see your coverage results when the tests are done running.

JSHint is a tool that will detect errors and violations of best practices in JavaScript. You can install a Karma plugin to run JSHint as part of your testing process.

You can configure your JSHint options in a .jshintrc file located at the root of your project. The file in the example project looks as follows. JSHint was designed to be configurable, so there are several other options available.

Now run the karma start test/karma-conf.js –single-run command. You should see a list of JSHint errors if you have any.

Karma has several useful plugins that can enhance your testing process. Be sure to check if there is one you could use or create your own. In the next post, I will explain how to set up Karma and Grunt, so you can integrate Karma with your build process.

Testing With AngularJS Part 2: Other Useful Karma Plugins

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.