I have a dead simple project (for the shake of asking this question). I used browserify to bundle my project, karma/jasmine as the testing framework, and browserify-instanbul for code coverage:
The problem is when I run npm test
, all the tests pass, terminal is green. But then when I check the coverage, there is nothing valuable there:
I can see that the unit tests testing mechanism work normally. I tried to make 1 test fail and it did reveal the failed test, however, the test coverage was still the same as the picture above.
Here are what I have:
Files under src folder:
animal.js
animal.spec.js
dog.js
dog.spec.js
Where the content of each source file can be simple as following:
animal.js:
function openMouth(){
return 'openMouth';
}
module.exports = {
openMouth: openMouth
};
dog.js:
var animal = require("./animal.js");
function say() {
animal.openMouth();
return 'woof';
}
module.exports = {
say: say
};
The specs files just verify the output of each function. So something like this:
dog.spec.js:
var dog = require("./dog.js");
describe('dog', function () {
it('should be able to say woof', function () {
expect(dog.say()).toBe('woof');
});
});
my karma.conf.js
module.exports = function(config) {
config.set({
basePath: '.',
autoWatch: true,
frameworks: ['jasmine', 'browserify'],
files: [
'src/**/*.js'
],
browsers: ['Chrome'],
reporters: ['progress', 'coverage'],
preprocessors: {
'src/**/*.spec.js': ['browserify'],
'src/**/!(*.spec).js': ['browserify']
},
singleRun: true,
plugins: [
'karma-coverage',
'karma-browserify',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine'
],
transform: [
['browserify-istanbul',
{
instrumenterConfig: {
embedSource: true // this is important for HTML reports
}
}
]
]
});
};
I fixed the issue by updating my karma config as following:
//jshint strict: false
module.exports = function(config) {
config.set({
basePath: '.',
autoWatch: true,
frameworks: ['jasmine', 'browserify'],
files: [
'node_modules/moment/moment.js',
'src/*.js'
],
browsers: ['Chrome'],
reporters: ['progress', 'coverage'],
preprocessors: {
'src/*.js': ['browserify']
},
browserify: {
//debug: true,
transform: ['browserify-istanbul']
},
coverageReporter: {
reporters : [
{"type": "text"},
{"type": "html", dir: 'coverages'}
]
},
singleRun: true,
plugins: [
'karma-coverage',
'karma-browserify',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine'
]
});
};
Remember to install instanbul
also.