Javascript browserify istanbul始终返回100%
我有一个非常简单的项目(为了问这个问题)。我使用browserify捆绑我的项目,karma/jasmine作为测试框架,browserify instanbul用于代码覆盖: 问题是当我运行npm测试时,所有测试都通过了,终端是绿色的。但当我检查保险范围时,没有任何有价值的东西: 我可以看到单元测试机制工作正常。我试图让1个测试失败,但它确实揭示了失败的测试,然而,测试覆盖率仍然与上图相同 以下是我所拥有的: src文件夹下的文件:Javascript browserify istanbul始终返回100%,javascript,unit-testing,code-coverage,browserify,Javascript,Unit Testing,Code Coverage,Browserify,我有一个非常简单的项目(为了问这个问题)。我使用browserify捆绑我的项目,karma/jasmine作为测试框架,browserify instanbul用于代码覆盖: 问题是当我运行npm测试时,所有测试都通过了,终端是绿色的。但当我检查保险范围时,没有任何有价值的东西: 我可以看到单元测试机制工作正常。我试图让1个测试失败,但它确实揭示了失败的测试,然而,测试覆盖率仍然与上图相同 以下是我所拥有的: src文件夹下的文件: animal.js animal.spec.js dog.
animal.js
animal.spec.js
dog.js
dog.spec.js
其中,每个源文件的内容可以简单如下:
//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'
]
});
};
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
};
specs文件只是验证每个函数的输出。比如说:
dog.spec.js:
var dog = require("./dog.js");
describe('dog', function () {
it('should be able to say woof', function () {
expect(dog.say()).toBe('woof');
});
});
我的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
}
}
]
]
});
};
我通过更新我的karma配置修复了这个问题,如下所示:
//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'
]
});
};
记住也要安装instanbul。摘要显示行(0/0)。似乎代码覆盖率工具设置不正确。我知道,这就是为什么我问如何正确配置它。