我们使用 Jasmine 进行 JavaScript 单元测试。我们有一个SpecRunner.html
文件来运行测试。是否存在一个工具,我可以将路径SpecRunner.html
和路径传递到 JavaScript(不是规范)文件的目录,它会生成一个 LCOV 报告。例如,像这样:
phantomjs jasmine_lcov.js SpecRunner.html WebContent/js
我们使用 Jasmine 进行 JavaScript 单元测试。我们有一个SpecRunner.html
文件来运行测试。是否存在一个工具,我可以将路径SpecRunner.html
和路径传递到 JavaScript(不是规范)文件的目录,它会生成一个 LCOV 报告。例如,像这样:
phantomjs jasmine_lcov.js SpecRunner.html WebContent/js
我同意@zaabalonso 的观点,即业力是正确的选择。由于您需要 LCOV 报告,因此您还需要karma-coverage插件,并且假设您想在 CI 中无头运行,您可能需要karma-phantomjs-launcher。通过 Grunt 运行是可选的,因为您始终可以使用 karma-cli ( ) 从命令行直接运行 karma npm install -g karma-cli
。
基本设置(使用 requireJS)如下所示:
包.json
{
"private": "true",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-jasmine-node": "^0.3.1",
"grunt-karma": "^0.10.1",
"jasmine-core": "^2.3.4",
"karma": "^0.12.32",
"karma-coverage": "^0.3.1",
"karma-jasmine": "^0.3.5",
"karma-phantomjs-launcher": "^0.1.4",
"karma-requirejs": "^0.2.2",
"requirejs": "^2.1.17"
}
}
karma.conf.js(注意preprocessors
和coverageReporter
部分
module.exports = function(config) {
config.set({
basePath: '.',
frameworks: ['jasmine', 'requirejs'],
files: [{
pattern: 'src/**/*.js',
included: false
}, {
pattern: 'spec/**/*.js',
included: false
},
"test-main.js"],
preprocessors: {
'src/**/*.js': ['coverage']
},
reporters: ['progress', 'coverage'],
coverageReporter: {
// specify a common output directory
dir: 'build/reports/coverage',
reporters: [
{ type: 'lcov', subdir: 'report-lcov' },
{ type: 'lcovonly', subdir: '.', file: 'report-lcovonly.txt' }
]
},
browsers: ['PhantomJS']
});
};
测试main.js
var allTestFiles = [];
var TEST_REGEXP = /^\/base\/spec\/\S*(spec|test)\.js$/i;
var pathToModule = function (path) {
return path.replace(/^\/base\//, '').replace(/\.js$/, '');
};
Object.keys(window.__karma__.files).forEach(function (file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
allTestFiles.push(pathToModule(file));
}
});
require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: '/base/',
enforceDefine: true,
xhtml: false,
waitSeconds: 30,
// dynamically load all test files
deps: allTestFiles,
callback: window.__karma__.start
});
Gruntfile.js(如果您想使用 Grunt,则可选)
module.exports = function(grunt) {
grunt.initConfig({
karma: {
unit: {
configFile: 'karma.conf.js',
options: {
singleRun: true
}
}
}
});
grunt.loadNpmTasks('grunt-karma');
grunt.registerTask('default', ['karma:unit']);
};
您可以使用karma start
. 这将启动业力服务器并运行一次测试。它将保持服务器正常运行,并在您修改源或测试源时重新运行测试。如果您只想运行一次测试(也许在 CI 中),您只需运行karma start --single-run
.
Chutzpah也会这样做。但是,它专注于 Windows 平台,因此可能对您有用,也可能不适用。这是完整的命令行选项文档,但您的命令可能是这样的:
chutzpah.console.exe SpecRunner.html /coverage /lcov coverage.dat
如果您需要微调覆盖范围排除或引用等内容,您可以在此处描述的测试区域中使用 json 配置文件位置。无需在命令行上指定被测 Javascript 代码的位置,因为 SpecRunner.html 中的引用会自动检测到该位置。
我发现 Chutzpah 非常流畅且易于使用。
我们在 grunt 上使用Karma,配置如下所示:
options = {
karma: {
unit: {
options: {
files: ['test/unit/specs/*.js'],
reporters: ['progress', 'coverage'],
preprocessors: {
'src/js/*.js': ['coverage']
},
coverageReporter: {
type : 'html',
dir : 'build/coverage/'
},
frameworks: ['jasmine'],
singleRun: true
}
}
}
}
你没有指定
SpecRunner.js
但是您可以为所有规范文件指定 *.js。
你可以运行它
咕噜的业力
这将生成与您显示的报告相似的报告。