我找到的解決方案是使用臨時配置文件。 代碼如下所示:
const tmp = require('tmp');
const fs = require('fs');
const gulp = require('gulp');
function createConfigFileContent(configurationObject) {
return `
module.exports = function karmaConfig(config) {
const conf = ${JSON.stringify(configurationObject)};
conf.logLevel = config.LOG_INFO;
return config.set(conf);
};
`;
}
function createConfigObject(testFiles) {
const files = testFiles.map(f => ({ pattern: f, included: true }));
return {
basePath: process.cwd(), // project root
frameworks: ['jasmine'],
files,
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
autoWatch: false,
browsers: ['Chrome'],
singleRun: true,
concurrency: Infinity,
};
}
function generateConfig(runner, testFiles) {
const config = createConfigObject(runner, testFiles);
return createConfigFileContent(config);
}
gulp.task('karma', done => {
// Create temporary configuration file
const config = tmp.fileSync();
// Populate it with config to run our files
fs.writeFileSync(config.name, generateConfig(FILESARRAY));
// Run karma
return new KarmaServer({
configFile: config.name,
singleRun: true,
},
function cleanUp() {
// Erase temporary config and finish task
config.removeCallback();
done();
}).start();
});
karma不提供此功能 – smnbbrv