(Unit) Testing really made the difference on any project I did. So every tool that can help me simplify or improve my testing experience, is a welcome addition to my tool belt. Recently I started using Karma, a test runner created by the Angular team.
From the website:
The main goal for Karma is to bring a productive testing environment to developers. The environment being one where they don't have to set up loads of configurations, but rather a place where developers can just write the code and get instant feedback from their tests.
Karma runs on Node.js and is available as an NPM package. Karma is essentially a tool which spawns a web server that executes source code against test code for each of the browsers connected. The results for each test against each browser are examined and displayed via the command line to the developer such that they can see which browsers and tests passed or failed.
After installing Karma through NPM (npm install karma --save-dev ), the first thing you need to is create a config file(karma init karma.conf.js). This command walks you through a set of questions to generate a config file specific for your application.
Next step is starting karma(karma start) to run your (Jasmine) tests. However my tests failed with the following error message:
ReferenceError: browser is not defined
Turns out that to get it working against Angular.js, there is one extra step you need to do. In the frameworks section you have to add ‘ng-scenario’ before ‘jasmine’:
// Karma configuration | |
// Generated on Fri Mar 27 2015 14:53:39 GMT+0100 (Romance Standard Time) | |
module.exports = function(config) { | |
config.set({ | |
// base path that will be used to resolve all patterns (eg. files, exclude) | |
basePath: '', | |
// frameworks to use | |
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter | |
frameworks: ['ng-scenario','jasmine'], | |
// list of files / patterns to load in the browser | |
files: [ | |
//'Scripts/angular.js', | |
'Scripts/test/*.js' | |
], | |
// list of files to exclude | |
exclude: [ | |
'Scripts/_references.js' | |
], | |
// preprocess matching files before serving them to the browser | |
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor | |
preprocessors: { | |
}, | |
// test results reporter to use | |
// possible values: 'dots', 'progress' | |
// available reporters: https://npmjs.org/browse/keyword/karma-reporter | |
reporters: ['progress'], | |
// web server port | |
port: 9876, | |
// enable / disable colors in the output (reporters and logs) | |
colors: true, | |
// level of logging | |
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG | |
logLevel: config.LOG_INFO, | |
// enable / disable watching file and executing tests whenever any file changes | |
autoWatch: true, | |
// start these browsers | |
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher | |
browsers: ['Chrome', 'IE'], | |
// Continuous Integration mode | |
// if true, Karma captures browsers, runs the tests and exits | |
singleRun: false | |
}); | |
}; | |