how to get the phantomjs working for karma testrunner? - angularjs

I am trying to run the karmatest runner with phantomJS in windows 7. This is the config file:
// Karma configuration
// http://karma-runner.github.io/0.12/config/configuration-file.html
// Generated on 2015-06-01 using
// generator-karma 0.8.3
module.exports = function (config) {
'use strict';
config.set({
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
//preprocessors: {
// 'app/**/*.js': ['coverage']
//},
//
preprocessors: {
'**/*.html': ['ng-html2js'],
'app/**/*.js': ['coverage']
},
reporters: ['progress', 'coverage'],
// base path, that will be used to resolve files and exclude
basePath: '../',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.4/angular.js',
'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.4/angular-mocks.js',
'bower_components/angular-animate/angular-animate.js',
'bower_components/angular-ui-router/release/angular-ui-router.js',
'bower_components/angular-bootstrap/ui-bootstrap.js',
'bower_components/angular-smart-table/dist/smart-table.min.js',
'bower_components/underscore/underscore.js',
'app/modules/**/*.js',
'app/modules/app.js',
'app/modules/config.js',
//'../app/scripts_old/**/*.js',
// 'test/mock/**/*.js',
'test/spec/**/*.spec.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: [
'PhantomJS'
],
// Which plugins to enable
plugins: [
'karma-phantomjs-launcher',
'karma-jasmine',
'karma-coverage',
'karma-chrome-launcher'
],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false,
colors: true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_DISABLE
// Uncomment the following lines if you are using grunt's server to run the tests
// proxies: {
// '/': 'http://localhost:9000/'
// },
// URL root prevent conflicts with the site root
// urlRoot: '_karma_'
});
};
I installed the karmaphantomjs launcher here:
https://www.npmjs.com/package/karma-phantomjs-launcher
I also set the phantomjs_bin environment variable to \phantomjs.exe
When I use the chromebrowser it works fine. How can I get it running for phantomJS?

Use the following process:
Remove the PHANTOMJS_BIN environment variable
Find the path to phantomjs via the cmd shell, then run:
where phantomjs
Copy the location to the the PATH environment variable by running path, then appending it to the list
Because you can override the phantomjs_bin path using env, that meant if you did that it would try to run the phantomjs script thru phantomjs.exe, not node.exe. This was fixable but led to more codepaths and I prefered to par things back to a simpler solution.
References
karma phantomjs-launcher fix: Switched from launching phantomjs via node to ensuring that the .exe is always launched
karma phantomjs-launcher source: index.js

Related

karma start - passing parameters

Is there a way to pass a parameter thru the Karma command line and then read that somewhere in your tests? For instance, this is what want:
karma start -branding="clientX"
And then somewhere in my specs I would need to access this variable (I need the "clientX" value).
Is this possible at all?
It is possible to transmit parameters to test cases. It can be a bit tricky. What you can to do is check for __karma__.config.args in you test suite:
it("get karma args", function () {
console.log(__karma__.config.args);
});
karma run
If you want to pass arguments with karma run, then the above is all you need.
Then if you do karma start and then karma run -- --foo you should see on the console:
LOG: ['--foo']
Note how the argument passed to karma run ended up in __karma__.config.args. Also note that the first double-dash in karma run -- --foo is there to separate Karma arguments from "client arguments" it is necessary. (karma start does not make the same distinction.)
karma start
karma start works differently.
If you use a default karma.conf.js created by karma init, you won't be able to pass arguments in this way by doing karma start --single-run --foo. You need to modify your karma.conf.js to pass the arguments:
module.exports = function(config) {
config.set({
client: {
args: config.foo ? ["--foo"] : [],
},
If you run karma start --single-run --foo, then you'll get the same input as with run earlier.
If I had to pass multiple arguments, I'd scan process.argv to filter out those parts of it that are only for Karma's benefit and pass the rest to args instead of testing for each possibility.
You may have inferred from the above that when you karma start --single-run --something the argument ends up as config.something in karma.conf.js.
Complete example
This example was tested against Karama 1.1.x and Karma 1.2.0. It shows the same method I've discussed above to get command line parameters to transit through client.args. This works both with karma start and karma run. I also added a method to pass values without going through client.args (that's the branding example). However, this method does not work with karma run.
karma.conf.js:
module.exports = function(config) {
config.set({
basePath: '',
client: {
// Example passing through `args`.
args: config.foo ? ["--foo"] : [],
// It is also possible to just pass stuff like this,
// but this works only with `karma start`, not `karma run`.
branding: config.branding,
},
frameworks: ['jasmine'],
files: [
'test/**/*.js'
],
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
test/test.js:
it("get karma arg", function () {
console.log("BRANDING", __karma__.config.branding);
console.log("ARGS", __karma__.config.args);
});
If you run karma start --single-run --foo --branding=q, you get:
LOG: 'BRANDING', 'q'
LOG: 'ARGS', ['--foo']
If you start Karma and then use karma run -- --foo --branding=q, you get:
LOG: 'BRANDING', undefined
LOG: 'ARGS', ['--foo', '--branding=q']
As mentioned above, when using karma run, everything must go through config.args to be visible in the test.
Yes it is possible. All you have to do is specify that parameter in client section of karma.conf.js :
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
plugins: [
...
],
client: { //Put the parameters here
codeCoverage: config.cc,
testSuite: config.testSuite
},
...
To pass it:
karma start --cc --testSuite=sanity
Note that if you don't give a value to parameter (like --cc) it will be set to true.
To access it from the tests:
console.log('Coverage: ', __karma__.config.codeCoverage);
console.log('Test suite: ', __karma__.config.testSuite);

karma/jasmine angular tests fails on osx

I have a big angular app whose tests have been running fine through grunt-karma/karma-jasmine since the beginning of the project. Recently, tests began to fail most of the time and I can't figure out what is going wrong.
I have a git commit that works every times, and the next one fails most of the times, and the next ones two. I've been fiddling with it for hours without being able to isolate anything that would make the tests pass on a consistent basis. Every time I think I have found what was tripping up the test suite, trying to use that knowledge a few commits later ends up in error anyway.
The first 25 tests always pass ok, and then I get an error message that doesn't bring much to the table :
Error: [$injector:modulerr] Failed to instantiate module app-module-common due to:
Error: [$injector:unpr] Unknown provider:
http://errors.angularjs.org/1.2.26/$injector/unpr?p0=
at /Users/hudson/workspace/app-recast-master/build/js/bottom/vendor/dev/20-angular.js:3802
The weird thing in this message is that no provider is specified as being unknown.
This is happening on the osx based box that is in charge of building the site, but not on my windows machine.
Here is what the karma.conf looks like:
(function () {
'use strict';
var exportedConf = require('./build.js');
var userConfig = exportedConf.userConfig;
module.exports = function (config) {
config.set({
// Karma configuration
// base path, that will be used to resolve files and exclude
basePath: '',
// frameworks to use
frameworks: ["jasmine"],
// list of files / patterns to load in the browser
files: [
'../' + userConfig.build_dir + '/js/top/vendor/dev/**/*.js',
//'../' + userConfig.build_dir + '/js/top/project-root/**/*.js',
'../' + userConfig.src_dir + '/fragments/config.js',
'../' + userConfig.build_dir + '/js/bottom/vendor/dev/**/*.js',
'../' + userConfig.build_dir + '/js/bottom/project-root/**/*.js',
'../test/mockFactory.js',
'../test/jasmineVersionCheck.js',
'../' + userConfig.project_dir + '/**/' + userConfig.tests_folderName + '/**/*.spec.js'
],
// list of files to exclude
exclude: [
],
preprocessors: {
// preprocessors are defined at the end of file so that we can use the userConfig variables in the key
},
// test results reporter to use
reporters: ['progress', 'junit', 'coverage'],
coverageReporter: {
dir: '../' + userConfig.reports_dir + '/',
reporters: [
{
type: 'cobertura',
file: 'coverage.xml'
},
{
type: 'html',
file: 'coverage.html'
}
]
},
// web server port
port: process.env.KARMA_PORT || 8080,
// cli runner port
runnerPort: process.env.KARMA_RUNNER_PORT || 9100,
junitReporter: {
outputFile: '../' + userConfig.reports_dir + '/test-results.xml'
},
// enable / disable colors in the output (reporters and logs)
colors: process.env.KARMA_COLORS || true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: [process.env.KARMA_BROWSER || 'PhantomJS'],
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 5000,
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true,
plugins: ['karma-jasmine', 'karma-phantomjs-launcher', 'karma-junit-reporter', 'karma-coverage']
});
// polyfills need to be excluded or instanbul instrumentation goes wild and screws it all!
config.preprocessors['../' + userConfig.build_dir + '/js/bottom/project-root/**/!(*-polyfills)+(.js)'] = ['coverage'];
};
}());
I hope someone can give me a hint on the matter, I have exhausted all the options I could think of.
Maybe, i really say, maybe, it might be due to the camel cased file names. IOs doesn't consider the case when it comes to file names.
I will edit my answer if i can come to any better idea.

Upgrading to AngularJS 1.3 from 1.2.6 unit test errors

I've been trying to upgrade our AngularJS app from 1.2.6 to 1.3.5 but have consistently been getting errors when trying to run the tests and the errors occur at test initialization. Further investigation proves that the tests are not even hit at the insertion points, rather fail just after initialization.
We are using Jasmine 1.x and karma test runner with Chrome as the browser. We are also using grunt to initiate the tests in this sequence:
grunt.registerTask('test', function () {
console.log(config);
grunt.task.run([
'jsbeautifier',
'newer:jshint',
'clean:server',
'autoprefixer',
'replace:htmlcommon',
'replace:htmlenv',
'replace:jscommon',
'replace:jsenv',
'to_single_quotes',
'connect:test',
'karma'
]);
});
Here is the error log:
LOG: 'WARNING: Tried to load angular more than once.'
Chrome 39.0.2171 (Mac OS X 10.10.1) ERROR
Uncaught TypeError: undefined is not a function
at /Users/devUser/Development/WebAppGroup/webapp/app/bower_components/angular/angular.js:25917
Chrome 39.0.2171 (Mac OS X 10.10.1) ERROR
Uncaught TypeError: undefined is not a function
at /Users/devUser/Development/WebAppGroup/webapp/app/bower_components/angular-resource/angular-
resource.js:8
Chrome 39.0.2171 (Mac OS X 10.10.1) ERROR
Uncaught TypeError: undefined is not a function
at /Users/devUser/Development/WebAppGroup/webapp/app/bower_components/angular-sanitize/angular-
sanitize.js:8
Chrome 39.0.2171 (Mac OS X 10.10.1) ERROR
Uncaught TypeError: undefined is not a function
at /Users/devUser/Development/WebAppGroup/webapp/app/bower_components/angular-route/angular-
route.js:26
Chrome 39.0.2171 (Mac OS X 10.10.1): Executed 0 of 0 ERROR (0.391 secs / 0 secs)
Excerpt of dependencies form our karma config file:
'app/scripts/common/googlemaps.js',
'app/bower_components/angular/angular.js',
'app/bower_components/angular-translate/angular-translate.js',
'app/bower_components/angular-translate-loader-partial/angular-translate-loader-partial.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-sanitize/angular-sanitize.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/momentjs/moment.js',
'app/bower_components/angular-touch/angular-touch.js',
'app/bower_components/angular-carousel/dist/angular-carousel.js',
'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js',
'app/bower_components/angular-scroll/angular-scroll.js',
'app/bower_components/jquery/dist/jquery.min.js',
'app/bower_components/jquery/dist/jquery.js',
'app/bower_components/angularitics/src/angulartics.js',
'app/bower_components/angularitics/src/angulartics-adobe.js',
'app/bower_components/angularitics/src/angulartics-chartbeat.js',
'app/bower_components/angularitics/src/angulartics-flurry.js',
'app/bower_components/angularitics/src/angulartics-ga-cordova.js',
'app/bower_components/angularitics/src/angulartics-ga.js',
'app/bower_components/angularitics/src/angulartics-gtm.js',
'app/bower_components/angularitics/src/angulartics-kissmetrics.js',
'app/bower_components/angularitics/src/angulartics-mixpanel.js',
'app/bower_components/angularitics/src/angulartics-piwik.js',
'app/bower_components/angularitics/src/angulartics-scroll.js',
'app/bower_components/angularitics/src/angulartics-segmentio.js',
'app/bower_components/angularitics/src/angulartics-splunk.js',
'app/bower_components/angularitics/src/angulartics-woopra.js',
'app/bower_components/angularitics/src/angulartics-marketo.js',
'app/bower_components/ngstorage/ngStorage.js',
'app/bower_components/ng-clip/src/ngClip.js',
'app/bower_components/zeroclipboard/dist/ZeroClipboard.js',
UPDATED Here is the entire karma config file:
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine', 'ng-scenario'],
// list of files / patterns to load in the browser
files: [
'app/scripts/common/googlemaps.js',
'app/bower_components/angular/angular.js',
'app/bower_components/angular-translate/angular-translate.js',
'app/bower_components/angular-translate-loader-partial/angular-translate-loader-partial.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-sanitize/angular-sanitize.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/momentjs/moment.js',
'app/bower_components/angular-touch/angular-touch.js',
'app/bower_components/angular-carousel/dist/angular-carousel.js',
'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js',
'app/bower_components/angular-scroll/angular-scroll.js',
'app/bower_components/jquery/dist/jquery.min.js',
'app/bower_components/jquery/dist/jquery.js',
'app/bower_components/angularitics/src/angulartics.js',
'app/bower_components/angularitics/src/angulartics-adobe.js',
'app/bower_components/angularitics/src/angulartics-chartbeat.js',
'app/bower_components/angularitics/src/angulartics-flurry.js',
'app/bower_components/angularitics/src/angulartics-ga-cordova.js',
'app/bower_components/angularitics/src/angulartics-ga.js',
'app/bower_components/angularitics/src/angulartics-gtm.js',
'app/bower_components/angularitics/src/angulartics-kissmetrics.js',
'app/bower_components/angularitics/src/angulartics-mixpanel.js',
'app/bower_components/angularitics/src/angulartics-piwik.js',
'app/bower_components/angularitics/src/angulartics-scroll.js',
'app/bower_components/angularitics/src/angulartics-segmentio.js',
'app/bower_components/angularitics/src/angulartics-splunk.js',
'app/bower_components/angularitics/src/angulartics-woopra.js',
'app/bower_components/angularitics/src/angulartics-marketo.js',
'app/bower_components/ngstorage/ngStorage.js',
/* 'app/bower_components/ng-clip/src/ngClip.js',
'app/bower_components/zeroclipboard/dist/ZeroClipboard.js',*/
'app/scripts/directives/setup.js',
'app/scripts/*.js',
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false,
reporters: ['progress', 'coverage'],
preprocessors: {
'app/scripts/**/*.js': ['coverage']
}
});
};
Basically upon installing all the dependencies via bower, bower complains that several of the dependencies don't agree with AngularJS 1.3.x, but rather it needs 1.2.x where x = minor version required by individual dependency. I've tried doing the 'majority rules' take and given 1.2.27 (where most of the dependencies were asking for that particular version) and I've tried brute force 1.3.5 upgrade.
As a last resort, I've also applied the '*' to the dependency in my bower.json file to get the latest version of each dependency to use with AngularJS 1.3.5, however this doesn't work as well.
The feature set works as I can start my angular app and work through most of the features, but when it comes to ur BDD tests, this fails.
I'm stuck at this point as I've tried all possible paths. I think this issue has something related to the extensive set of libraries we are using and it's individual requirements for an older version of AngularJS. Also, why is it trying to load AngularJS more than once? I've read up on this being a possibility due to bad or missing routes but our routes work well when the application is started up.
Any thoughts?
Thanks,
Ben.

Issue with Karma runner executing test cases

I am developing an application which uses Karma as testing tool for executing test cases. The problem is when I try to execute test cases it isn't executing any tests rather it just stuck at a point. When I use this command
grunt test --force
I am getting this result
Running "clean:server" (clean) task
Running "concurrent:test" (concurrent) task
Running "compass:dist" (compass) task
Running "compass:server" (compass) task
Done, without errors.
Running "connect:test" (connect) task
Warning: Arguments to path.resolve must be strings Used --force, continuing.
Running "karma:unit" (karma) task
INFO [karma]: Karma v0.10.10 server started at http://localhost:9876/
After that no progress, it just stuck here. Here is my karma.conf.js file
// Karma configuration
// base path, that will be used to resolve files and exclude
basePath = '';
module.exports = function(config) {
config.set({
frameworks: ['jasmine']
});
};
// list of files / patterns to load in the browser
files = [
'bower_components/angular/angular.min.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-animate/angular-animate.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'bower_components/ng-grid/lib/jquery-1.9.1.js',
'bower_components/ng-grid/ng-grid-2.0.7.min.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-bootstrap/ui-bootstrap.js',
'bower_components/momentjs/moment.js',
'bower_components/nprogress/nprogress.js',
'bower_components/toastr/toastr.js',
'app/*.js',
'app/**/*.js',
'../sinonjs/test.js'
];
// list of files to exclude
exclude = ['app/out.js'];
preprocessors = {
'app/*.js': 'coverage',
'app/**/*.js': 'coverage'
};
// test results reporter to use
// possible values: dots || progress || growl
// for test coverage2
reporters = ['progress', 'coverage'];
coverageReporter = {
type : 'html',
dir : 'coverage/'
}
// test results reporter to use
// possible values: dots || progress || growl
//reporters = ['progress'];
// web server port
port = 8080;
// cli runner port
runnerPort = 9100;
// enable / disable colors in the output (reporters and logs)
colors = true;
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
//logLevel = LOG_INFO;
// enable / disable watching file and executing tests whenever any file changes
autoWatch = false;
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers = ['Chrome'];
// If browser does not capture in given timeout [ms], kill it
captureTimeout = 5000;
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun = false;
What might be the issue here?
When I added a logLevel configuration in my karma.conf.js file I got the following result
Running "clean:server" (clean) task
Running "concurrent:test" (concurrent) task
Running "compass:dist" (compass) task
Running "compass:server" (compass) task
Done, without errors.
Running "connect:test" (connect) task
Warning: Arguments to path.resolve must be strings Used --force, continuing.
Running "karma:unit" (karma) task
DEBUG [plugin]: Loading karma-* from E:\wamp\www\balto4forms\node_modules
DEBUG [plugin]: Loading plugin E:\wamp\www\balto4forms\node_modules/karma-chrome
-launcher.
DEBUG [plugin]: Loading plugin E:\wamp\www\balto4forms\node_modules/karma-coffee
-preprocessor.
DEBUG [plugin]: Loading plugin E:\wamp\www\balto4forms\node_modules/karma-firefo
x-launcher.
DEBUG [plugin]: Loading plugin E:\wamp\www\balto4forms\node_modules/karma-html2j
s-preprocessor.
DEBUG [plugin]: Loading plugin E:\wamp\www\balto4forms\node_modules/karma-jasmin
e.
DEBUG [plugin]: Loading plugin E:\wamp\www\balto4forms\node_modules/karma-phanto
mjs-launcher.
DEBUG [plugin]: Loading plugin E:\wamp\www\balto4forms\node_modules/karma-requir
ejs.
DEBUG [plugin]: Loading plugin E:\wamp\www\balto4forms\node_modules/karma-script
-launcher.
INFO [karma]: Karma v0.10.10 server started at http://localhost:9876/
DEBUG [watcher]: Resolved files:
E:/wamp/www/balto4forms/node_modules/karma-jasmine/lib/jasmine.js
E:/wamp/www/balto4forms/node_modules/karma-jasmine/lib/adapter.js
I resolved it myself by wrapping up all the configuration settings under the module.exports function. Now my karma.conf.js file will look like this;
// Karma configuration
// base path, that will be used to resolve files and exclude
module.exports = function(config) {
config.set({
basePath : '',
frameworks: ['jasmine'],
logLevel: config.LOG_INFO,
// list of files / patterns to load in the browser
files : [
'bower_components/angular/angular.min.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-animate/angular-animate.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'bower_components/ng-grid/lib/jquery-1.9.1.js',
'bower_components/ng-grid/ng-grid-2.0.7.min.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-bootstrap/ui-bootstrap.js',
'bower_components/momentjs/moment.js',
'bower_components/nprogress/nprogress.js',
'bower_components/toastr/toastr.js',
'app/*.js',
'app/**/*.js',
'../sinonjs/test.js'
],
// list of files to exclude
exclude : ['app/out.js'],
preprocessors : {
'app/*.js': 'coverage',
'app/**/*.js': 'coverage',
},
// test results reporter to use
// possible values: dots || progress || growl
// for test coverage2
reporters : ['progress', 'coverage'],
coverageReporter : {
type : 'html',
dir : 'coverage/'
},
// test results reporter to use
// possible values: dots || progress || growl
//reporters : ['progress'];
// web server port
port : 8080,
// cli runner port
runnerPort : 9100,
// enable / disable colors in the output (reporters and logs)
colors : true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
//logLevel : LOG_INFO;
// enable / disable watching file and executing tests whenever any file changes
autoWatch : false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers : ['Chrome'],
// If browser does not capture in given timeout [ms], kill it
captureTimeout : 5000,
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun : false
});
};
Now all the test cases are executing fine.

Karma coverage always shows 100%

My karma.conf.js files looks like this :
// Karma configuration
// Generated on Tue Jun 11 2013 14:14:12 GMT+0100 (GMT Daylight Time)
// base path, that will be used to resolve files and exclude
basePath = '';
// list of files / patterns to load in the browser
files = [
JASMINE,
JASMINE_ADAPTER,
'../Scripts/angular/angular.js',
'../Scripts/angular/restangular/underscore-min.js',
'../Scripts/angular/restangular/restangular-min.js',
'../Scripts/angular/angular-*.js',
'../Scripts/angular/angular-test/angular-*.js',
'../Scripts/angular/angular-ui/*.js',
'../Scripts/angular/angular-strap/*.js',
'../Scripts/angular/angular-http-auth/*.js',
'../uifw/scripts/ui-framework-angular.js',
'../app/app.js',
'../app/**/*.js',
'unit/**/*.js'
];
// list of files to exclude
exclude = [
'../Scripts/angular/angular-test/angular-scenario.js'
];
preprocessors = {
'**/../app/**/*.js': 'coverage'
};
coverageReporter = {
type: 'html',
dir: 'coverage/'
};
// test results reporter to use
// possible values: 'dots', 'progress', 'junit'
reporters = ['progress', 'coverage'];
// web server port
port = 9876;
// cli runner port
runnerPort = 9100;
// enable / disable colors in the output (reporters and logs)
colors = true;
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel = LOG_DEBUG;
// enable / disable watching file and executing tests whenever any file changes
autoWatch = true;
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers = ['Chrome'];
// If browser does not capture in given timeout [ms], kill it
captureTimeout = 60000;
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun = false;
My folder structure looks like this :
Root
|__ App
|__ Scripts
|__ Tests
|__ .... other folders
Karma.conf.js is located inside the tests folder. Karma start karma.conf.j is run from within the tests folder.
My tests run and a coverage folder is created, but coverage always shows 100%.
What am I doing wrong?
EDIT:
In fact it turned out to be a simple answer. the preprocessors = {
'**/../app/**/*.js': 'coverage'
};
no longer needs to be prefixed with **
See this for more details
Set up webpack with istanbul-instrumenter-loader got me on the right track.
{
test: /\.ts/,
include: helpers.root('src', 'app'),
loader: 'istanbul-instrumenter-loader',
enforce: 'post'
}
You need to set ANGULAR_SCENARIO_ADAPTER. Karma doesn't use JASMINE_ADAPTER.
To me it seems like your problem is that you are unable to find any of the files you are trying to generate coverage for. Your report does say that 0/0 lines are ran, 0/0 statements and so on.
I had a similar problem that was caused by me using lower case letters instead of upper case ones and it seems you might have the same problem as you have a folder named "App" but you reference it with "app" in the config. See if you can get it to work by writing **/../App/**/*.js': 'coverage'
Also I am not sure but I think you should write ../App/**/*.js': 'coverage' instead of **/../App/**/*.js': 'coverage'

Resources