【发布时间】:2017-03-17 08:24:00
【问题描述】:
我有一个非常简单的项目(为了问这个问题而感到震惊)。我使用 browserify 来捆绑我的项目,使用 karma/jasmine 作为测试框架,使用 browserify-instanbul 来进行代码覆盖:
问题是当我运行npm test 时,所有测试都通过了,终端是绿色的。但是当我检查覆盖范围时,那里没有任何有价值的东西:
我可以看到单元测试测试机制正常工作。我尝试让 1 次测试失败,它确实显示了失败的测试,但是,测试覆盖率仍然与上图相同。
这是我所拥有的:
src文件夹下的文件:
animal.js
animal.spec.js
dog.js
dog.spec.js
其中每个源文件的内容可以简单如下:
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
}
}
]
]
});
};
【问题讨论】:
-
摘要显示行数 (0/0)。似乎代码覆盖率工具没有正确设置。
-
我知道,这就是为什么我问如何正确配置它。
标签: javascript unit-testing code-coverage browserify