gpt4 book ai didi

javascript - karma : how to change preprocessors on commandline (or switch preprocessors in config)

转载 作者:行者123 更新时间:2023-11-30 17:19:54 25 4
gpt4 key购买 nike

我想用不同的预处理器运行 Karma 几次。基于失败,karma exec 在命令行上监听 --preprocessors,但我无法正确设置它。

以下都返回相同的错误。

karma start --single-run web-app/karma.conf.js --preprocessors "{\"../grails-app/assets/javascripts/**/!(lib)/**/*.js\": \"jshints\"}"
karma start --single-run web-app/karma.conf.js --preprocessors {"../grails-app/assets/javascripts/**/!(lib)/**/*.js": "jshints"}
karma start --single-run web-app/karma.conf.js --preprocessors "{'../grails-app/assets/javascripts/**/!(lib)/**/*.js': 'jshints'}"

错误:

/usr/lib/node_modules/karma/lib/config.js:145
Object.keys(preprocessors).forEach(function(pattern) {
^
TypeError: Object.keys called on non-object
at Function.keys (native)
at normalizeConfig (/usr/lib/node_modules/karma/lib/config.js:145:10)
at Object.parseConfig (/usr/lib/node_modules/karma/lib/config.js:293:10)
at Object.exports.start (/usr/lib/node_modules/karma/lib/server.js:282:20)

我为什么要这样做,还有其他选择吗?

coveragejshint 预处理器不兼容。我可以复制 karma.conf.js 但这不是可维护性的长期选择。

最佳答案

创建一个 karma.conf.js 模板。

module.exports = {
...
}

为 karma 创建一个包装器(我们称之为“wrapper.js”):

var karma = require('karma');

function configurator(options){
var config = getTemplate();

// based on the options object will add different preprocessors
if(options.blah){
config.preprocessors["../grails-app/assets/javascripts/**/!(lib)/**/*.js"] = 'whatever';
}
return config;
}

function getTemplate(){
return {
// start with an empty object
preprocessors: {},
// point to the template, we will enrich it
configFile : __dirname + 'path/to/your/karma.conf.js'
};
}

function startKarma(options){
var config = configurator(options);
karma.server.start(config, function(exitCode){
// exit code === 0 is OK
if (!exitCode) {
console.log('\tTests ran successfully.\n');
// rerun with a different preprocessor
startKarma({blah1: true});
} else {
// just exit with the error code
process.exit(exitCode);
}
});
}

function passedArg(string){
// look at the arguments passed in the CLI
return process.argv.indexOf(string) > -1;
}

function start(){
// start with empty options
var options = {};

if(passedArg('blah')){
options.blah = true;
}
//start karma!
startKarma(options);
}

start();

此时可以从控制台传参:

$ node wrapper.js blah

有关 karma API 的更多信息,请查看:http://karma-runner.github.io/0.8/dev/public-api.html

关于javascript - karma : how to change preprocessors on commandline (or switch preprocessors in config),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25335296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com