code-coverage,karma-runner,browserify,ecmascript-6,karma-coverage , Getting Karma, 6to5ify and Istanbul to play ball

Getting Karma, 6to5ify and Istanbul to play ball


Tag: code-coverage,karma-runner,browserify,ecmascript-6,karma-coverage

I have Browserify, 6to5ify and Karma to play nice, successfully running my specs. When I add code coverage however, things go south. I've tried several approaches:

Right now my karma.conf.js looks like this:

module.exports = function(karma){
    frameworks: ["browserify", "mocha", "chai-sinon"],
    browserify: {
      debug: true,
      extensions: [".js", ".hbs"],
      transform: ["6to5ify", "hbsfy"]
    reporters: ["dots", "osx", "junit", "coverage"],
    coverageReporter: {
      type: "text"
    junitReporter: {
      outputFile: "spec/reports/test-results.xml"
    preprocessors: {
      "src/javascript/**/*": ["coverage"],
      "spec/**/*": ["browserify"]
    browsers: ["PhantomJS"],
    files: ["spec/unit/**/*Spec.js"],
    logLevel: "LOG_DEBUG",
    autoWatch: true

I'm kind of lost how to get this all working together. I tried following these instructions, but that didn't work because it didn't follow my browser node in package.json. Any help would be greatly appreciated.


So, apparently I need browserify-istanbul, and I need the browserify configure hook, like so:

var to5ify = require('6to5ify');
var hbsfy = require('hbsfy');
var cover = require('browserify-istanbul');

var coverOptions = {
  ignore: ['**/*Spec.js', '**/lib/*.js', '**/fixtures/*.hbs'],
  defaultIgnore: true

module.exports = function(karma){
    frameworks: ["browserify", "mocha", "chai-sinon"],
    browserify: {
      debug: false,
      extensions: [".js", ".hbs"],
      configure: function(bundle){
        bundle.on('prebundle', function(){
    reporters: ["dots", "osx", "junit", "coverage"],
    coverageReporter: {
      type: "text"
    junitReporter: {
      outputFile: "spec/reports/test-results.xml"
    preprocessors: {
      "spec/**/*": ["browserify"]
    browsers: ["PhantomJS"],
    files: ["spec/unit/**/*Spec.js"],
    logLevel: "LOG_DEBUG",
    autoWatch: true


I get an error — Error: [$injector:nomod]$injector/nomod?p0=decisionTree when I run karma.cong.js in Webstorm

Here is my karma.conf.js file module.exports = function(config) { var configuration = { basePath : '', autoWatch : false, frameworks: ['jasmine'], files: [ 'bower_components/angular/angular.min.js', 'bower_components/angular-mocks/angular-mocks.js', 'bower_components/angular-resource/angular-resource.min.js', 'bower_components/angular-animate/angular-animate.min.js', 'bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js', 'bower_components/angular-cookies/angular-cookies.min.js',...

Visual studio does not exist in the namespace, but reference is added

I am building a custom code coverage app using the Microsoft.VisualStudio.Coverage.Analysis class. I have added the reference to the dll to my solution already and when I start typing the using statement at the top of my class Visual Studio is predicting the correct names, e.g. after typing "Microsoft." "VisualStudio"...

Trouble running a scope function in angular test

I have recently started trying to learn how to test angular. I have had some success but at the moment I am trying to test a directive and I cannot seem to make it work. Test code describe('navigation test', function(){ var element, location, route, rootScope, scope, httpBackend; var elm; beforeEach(module('myApp'));...

How does Visual Studio measure the % Blocks coveraged in test unit?

When I click to run the unit tests with code coverage, it has a column showing the % of blocks coveraged. I also use TeamCity to measure, and I noticed that they have different metric. I would like to know how Visual Studio measures. I did some calculations but I...

Setup karma to test a node.js module?

I'm developing a Node.js module, and I want to use Karma to auto-test it while working. In my config file, I setup this: // list of files / patterns to load in the browser files: [ './index.js', './test/indexSpecs.js' ], Obviously, since Node.js isn't included in the browser files, I get...

Supress console logs from code being tested

I'm unit testing a function which contains a console.log() call. How can I prevent the output of that call from being displayed in the output console of my test runner, in this case, Karma. I'm using Jasmine. I'm looking for a more elegant way than overriding browser's console methods, preferably...

Karma not starting phantomJs

I'm loosing my mind here. I have a gulp task that looks like this. var gulp = require("gulp"); var karma = require("karma").server; gulp.task("test", function (done) { karma.start({ configFile: __dirname + "/karma.conf.js", action:"run" }, function () { done(); }); }); My karma.conf.js looks like this: module.exports = function (config) { config.set({...

Code Coverage for Android Library Project while using Robolectric

I have an android library project. We are using Robolectric to run the unit tests. Unit Tests are working fine from the IDE as well as Ant build.xml file. I am able to get the code coverage using the eclEmma plugin in Eclipse. But I am facing problems while getting...

How to ignore a run.js in Angular / Karma unit testing?

I have a file run.js that has:[ '$rootScope', '$location', 'cache', 'userCache', function($rootScope, $location, cache, userCache) { userCache.checkSession().then(function(result) { if(result.userId) { cache.set('login', 'successfulLogin', true); if($location.path() !== '/patients') { $location.path('/patients'); } } }); } ]); This runs when the app loads. This is normally great behavior. But when I'm unit testing...

Can I use karma to launch a test server?

I have setup testing locally using Karma, Mocha and Chai, whenever I run karma start I get a webserver running locally: INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/ I can then go to http://localhost:9876 using any browser I have and see how my tests run on that browser. Is...

Gulp task not accessing karma.conf.js file

I am using following gulp task to execute the karma/jasmine unit test cases . however i was not able access the karma.conf.js file for some reason. this problem occurs when i use this pathpackage to access the karma.conf.js file. gulp task gulp.task('tdd', function (done) { karma.start({ configFile: fs.readFile(path.join(__dirname, '../Tests/karma.conf.js')), singleRun:...

Mock the value of a service inside directive's unit test

Inside my directive's instantiation I have a call to a method that returns some data: data = myService.getData() and later in the code I am doing something = data[id] In karma I have an error, cannor read id of undefined, meaning that data is undefined. I tried to mock the...

Inconsistent behaviour with browserstack testing of ReactJS via travis-ci vs localhost

I'm running test towards browserstack with karma (karma-browserstack-launcher, browserify + reactify), using jasmine as test suite with my reactjs code, and I'm getting inconsistent behaviour. If I run karma start karma.conf from my laptop (using Chrome 39 on windows 8.1), every test runs fine, but if I the command is...

How to invoke spyOn on a scope function

I have the following jasmine spec. describe('ViewMeetingCtrl', function () { var $rootScope, scope, $controller , $q ; beforeEach(angular.mock.module('MyApp')); beforeEach(inject(function ($rootScope, $controller ) { scope = $rootScope.$new(); createController = function() { return $controller('ViewMeetingCtrl', { $scope: scope, meeting : {} }); }; })); it('the meeting type should be equal to an object',...

Send existing LCOV file to SonarQube

Because of the way my project is built, I can't use SonarQube to run coverage on my project. I have my JavaScript coverage all working with Karma and other tools. These tools output a valid LCOV file. Everything else I've found requires you to have SonarQube run the coverage and...

how to mock $state.params in jasmine unit testing

I have the following controller EditMeetingCtrl.js (function() { 'use strict'; angular .module('myApp') .controller('EditMeetingCtrl', EditMeetingCtrl); EditMeetingCtrl.$inject = ['$rootScope', '$scope', '$state', '$http', '$translate', 'meetingService', 'companyService', 'notificationService', 'meeting']; function EditMeetingCtrl($rootScope, $scope, $state, $http, $translate, meetingService, companyService, notificationService, meeting) { $scope.meeting = meeting; $scope.companyId = $state.params.companyId; $ = save; function...

Asynchronous tasks in grunt.registerTask

I need to call two functions within grunt.registerTask, but the second function has to be called after the first function is done. So I was wondering if we can use callbacks or promises or other asynchronous mechanisms within grunt.registerTask. (More specifically, I need to launch karma in my first function...

AngularJS, Mocha, Karma. testing controller, mocking service promise

I'm using AngularJS, Karma, Mocha, Chai, Chai-as-promised, Sinon. I'm trying the controller below. I am having trouble mocking the service User and test the .then() sections in the controller. Controller Code .controller('SignupPhoneCtrl', function(User, $scope) { $scope.sendPhoneNumber = function(countryCode, phoneNumber){ User.sendPhoneNumber(countryCode, phoneNumber) .then(function(result){ if(result == "success"){ //I WANT TO TEST THIS...

Accessing scope.variables inside a function in jasmine

I am trying to get the assert the value of the $scope.buttonDisable variable which is inside the save(). here is the test case describe('', function () { var $rootScope, scope, $controller , $q , state , controller ; var companyService , meetingService ; beforeEach(angular.mock.module('MyApp')); beforeEach(angular.mock.inject(function (_$httpBackend_, _companyService_ , _meetingService_ )...

jacoco showing code coverage is 0

I have below pom.xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18</version> <configuration> <!-- Sets the VM argument line used when unit tests are run. --> <argLine>-Xms512m -Xmx1024m -XX:MaxPermSize=512m ${jacoco.agent.argLine}</argLine> </configuration> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId>...

lodash npm distribution in browser

I'm using lodash in my tests, Not using webpack or browserify (don't need to). I found that the default npm build differs from what's in bower. I could of course use bower version, but I'm curious if I can use npm's instead (it's already there for Gulp). For bower I...

How to increase the coverage on testing angular directive?

So here is my angular directive. Simple one that uses a template url angular.module('my.directives') .directive('userNameDisplay', function() { return { restrict: 'E', scope: { user: '=user', status: '=status' }, templateUrl: '/partials/userNameDisplay.html' }; }); The spec is as follows. Again it tries to cover all cases. describe('user-name-display', function () { var elm,...

Alternatives for for loops in R?

I got 2 files which I'd like to combine using R. head(bed) chr8 41513235 41513282 ANK1.Exon1 chr8 41518973 41519092 ANK1.Exon2 The first one is giving intervals and their names. (Chromosome, from, to, name) head(coverage) chr1 41513235 20 chr1 41513236 19 chr1 41513237 19 The second one is giving coverages for...

Reusing angular mocks in Jasmine tests using $provide

I wish to reuse my mocks instead of having to set them up in every unit test that has them as dependency. But I'm having a hard time figuring out how to inject them properly. Here's my attempt at unit test setup, which of course fails because ConfigServiceMockProvider doesn't exist....

Roslyn - dynamic (runtime) flow

I started playing with Roslyn. It’s relatively easy to parse code and do static analysis. I wonder if it’s possible to use it for runtime analysis? I want to call a method with parameters and check which branches were executed. In other words, I need a runtime execution plan. Is...

Using $injector in AngularJS when integration testing (without using ngMock)

I'm needing to setup some integration tests in AngularJS using Karma/Jasmine but having trouble, because when not using ngMock (since I want to hit the actual $http endpoints), there are no module or inject methods. So how do I inject services into my tests? I've tried angular.injector.invoke(...) but can't get...

React's TestUtils.Simulate.keyDown does not work

I have a lot of components in my application that respond to different key presses, and so far, none of my tests that use TestUtils.Simulate.keyDown work at all. It seems like keyDown just plain and simple does not work. Here's the component I'm trying to test: description.js var React =...

How to mock $scope.variables in jasmine

I have the following test case CompanyCtrlSpec.js describe('ViewCompanyCtrl', function () { var $rootScope, scope, $controller , $q ; beforeEach(angular.mock.module('MyApp')); beforeEach(inject(function ($rootScope, $controller ) { scope = $rootScope.$new(); createController = function() { return $controller('ViewCompanyCtrl', { $scope: scope, company : {} }); }; })); it('the company type should be equal to an...

Spy on a service method call using jasmine Spies

I have the following controller ViewMeetingCtrl.js (function () { 'use strict'; angular.module('MyApp').controller('ViewMeetingCtrl', ViewMeetingCtrl); ViewMeetingCtrl.$inject = ['$scope', '$state', '$http', '$translate', 'notificationService', 'meetingService', '$modal', 'meeting', 'attachmentService']; function ViewMeetingCtrl($scope, $state, $http, $translate, notificationService, meetingService, $modal, meeting, attachmentService) { $scope.meeting = meeting; $scope.cancelMeeting = cancelMeeting; function cancelMeeting(meetingId,...

Karma - Load Files From Directories First

My karma files config: files: [ ... app/scripts/**/*.js ... ], I have 4 directories under scripts directory, and app.js. I need the app.js to be loaded last (since it depends on other scripts). Any way to ensure this?...

Unit testing with Karma on an Angular 1.3/JSPM/Babel project

So I'm setting up an angular 1.3 es6 project using jspm and babel. Im using es6's import/export feature. I have karma and karma-jspm installed and can run basic tests without an issue. When I try to import one of my modules into a test to test it, I get an...

how to fix images causing “karma WARN [web-server]: 404”?

I have an angular directive with a template that is doing something like: <img ng-src="{{url}}" /> In my test, I am setting the scope so that url points to a fictitious image... Karma then says: WARN [web-server]: 404: /fake.png So I go into my karma config file and add to...

Karma: angular is not defined

I've downloaded angular through bower and have included it in files in the conf file. File Structure/Files. When I run karma, the tests say: angular is not defined. I thought including it in the Files section was supposed to resolve it as a dependency. Any thoughts?...

Running Karma and Protractor tests with or without a headless browser

I am confused about running Karma and Protractor on a TeamCity CI server. Should I run these tests with a headless browser or not and how can I do that?

Angular test using $httpBackend fails with “400 thrown” error

For hours I've been trying to test my NewPostController with $httpBackend. The problem is whenever I set non-2xx status code in the response, the test fails. NewPostController has the following method: $scope.submit = function () { var newPost = $scope.newPost; PostService.insertPost(newPost).then(function (post) { $location.path("/my-posts"); }, function (status) { $scope.form.$setPristine(); $scope.error...

Generating Define-Use Paths for C Code coverage analysis

How is it possible to generate uncovered Define-Use Paths for C Code (using e.g. gcc). As I saw this subject is only academic. (unlike line coverage) resource:

Speed up tests run in Chrome

This is based on this question where it was discovered that when Chrome is not in the foreground, the tests run really slow. Anyone know how to get around that so I can continue to use Chrome, but don't have to keep it in the foreground?

Angular, Sinon, promises

Can't get my head around angular, sinon and promises. If I need to test something like this: myCtrl.js angular.module('app') .controller('myCtrl', ($scope, dataService)=> { dataService.list('location').then((lst)=> { $scope.list = lst; }); }); myCtrl-spec.js describe('testing controller', ()=> { var locations = ['A','B','C'], dataService, $scope; beforeEach(module('app')); beforeEach(inject($controller, $rootScope, _dataService_, $q) => { dataService =...

Karma tests reporting fast runs, but actually running slow

This is best explained with a video (or see the gif below): You'll notice that the Karma progress reporter is reporting that the tests are taking milliseconds, but it's obviously taking quite a while... I tweeted about this and was told to Try opening the debug page, and open the...

Using Istanbul and Mocha to cover ES6 code

I have Node code, written in ES6, that I test by issuing mocha --harmony. Tests are fine - everything works. Now I want to add coverage and istanbul to the mix, but I keep getting errors on the first arrow function encountered: No coverage information was collected, exit without writing...

Exporting coverage data from build machine

I was working on team internal KPI's lately and I thought that code coverage/number of unit tests will be a great measure. In our CI build pipeline we're using NUnit to run test and JetBrains dotCover to generate coverage report - everything works great and we have that fancy "statistics"...

Code / Test coverage tool for cocoapods?

Is there a tool out there that can check the coverage of a cocoapod repository? I've taken a look at codeclimate, gcov, gcovr, XcodeCoverage, cocoapods-coverage, but none of them will check the coverage of the cocoapod files, just the default project's classes....

Karma + Angular undefined error

I just started angular testing with karma and jasmine. I've written two basic tests. One of the two tests pass, but the other one is failing. I can't seem to debug it. I've been looking everywhere, and it should work according to various tutorials. The $scope variable should be available....

Nodejs : peerDependencies error while installing depedencNodejs

In my attempt to get the libraries I need, npm install was working well till about a year and now as I move to a new server it breaks stating PeerDependencies error. I am kind of confused as I fail the reason for which the code is breaking, has any...

How to inject a service to jasmine

I have the following test case MeetingCtrlSpec.js describe('ViewMeetingCtrl', function () { var $rootScope, scope, $controller ; beforeEach(angular.mock.module('MyApp')); beforeEach(inject(function ($rootScope, $controller ) { scope = $rootScope.$new(); $controller('ViewMeetingCtrl', { $scope: scope, }); })); it('should change greeting value if name value is changed', function () { //some assertion }); }); ViewMeetingCtrl.js (function ()...

How to setup perl to always run code coverage

I'd like to setup my project to always run the scripts through perl's Devel::Cover module. I've tried replacing the perl binary with something like this #!/bin/sh exec /usr/local/bin/original-perl -MDevel::Cover [email protected] with no success. I'd like to avoid modifying the #! for every script on my system (not the end of...

How can I unit test a promise chain with Karma?

My controller code has: save: function () { var that = this; patientCache.saveCurrentPatient().then(function(){ return adherenceCache.updateAdherenceSchedule(that.model.patientId) }).then(function () { that.buildAdherenceUrl(); }); }, I want to test patientCache.saveCurrentPatient(), adherenceCache.updateAdherenceSchedule and that.buildAdherenceUrl are called. This is my test: beforeEach(function() { module('mapApp'); return inject(function($injector) { var $controller, $rootScope; $rootScope = $injector.get('$rootScope'); $controller =...

Writing the most basic Unit test in Angular 2?

Problem: As soon as I import Angular 2 into a file none of my tests execute. Question: How can I set up my karma config to support angular two so my test pass properly? OR Question: How can I set up any testing framework with angular2 written in es6? Git...

Executed 0 of 0 ERROR when running Karma on Cordova project

I have an apache cordova project setup and I'm trying to use Karma for the first time. I've started the project like this: karma start test/karma.conf.js --verbose But all I'm getting is: INFO [karma]: Karma v0.12.36 server started at http://localhost:9876/ INFO [launcher]: Starting browser Chrome WARN [web-server]: 404: /favicon.ico INFO...