1

我没有看到任何关于此的内容,但我想知道是否可以在单个 Karma 运行中包含多个 karma-conf.js 文件?基本上,我正在考虑为 CI 配置一个具有覆盖率和 tslint 的配置,以及一个仅针对本地开发进行测试的配置。

4

2 回答 2

9

karma.conf.child.js这是一个引用主文件的子文件的简单示例karma.conf.js。子文件导入主要设置,进行更改,然后再次导出。您也可以将此导入-更改-导出模式与其他工具一起使用。

在此示例中,我更改了设置以在 TeamCity CI 服务器上启用报告:

// This is karma.conf.child.js

// import settings from default config file
var properties = null;
var originalConfigFn = require('./karma.conf.js');
originalConfigFn({ set: function(arg) { properties = arg; } });

// alter settings here:
properties.colors = false;
properties.singleRun = true;
properties.autoWatch = false;
properties.browsers = ['ChromeHeadless'];
properties.reporters.push('teamcity');
properties.coverageIstanbulReporter.reports.push('teamcity');

// export settings
module.exports = function (config) {
  config.set(properties);
};

要运行,请按照文档调用子配置文件:

karma start ./src/karma.conf.child.js

我正在使用 Angular CLI,所以我这样调用ng test

ng test --karma-config=./src/karma.conf.child.js
于 2018-09-03T11:11:23.060 回答
0

As far as I know Karma only accepts a single configuration file as input, that can be specified as command line argument or as part of the constructor options of the karma server.

Maybe gulp offers an alternative approach for you. I use a gulp file with several tasks. Each task is able to override the default configuration that is given in karma-conf.js, e.g.

console.log('Importing gulp dependencies ...');

var gulp = require('gulp');
var karma = require('karma');
var KarmaServerConstructor = karma.Server;
var karmaStopper = karma.stopper;
var watch = require('gulp-watch');
var commandLineArguments = require('yargs').argv;
var svn = require('gulp-svn');
var exec = require('child_process').exec;
var fs = require('fs');
var os = require('os');
var open = require('gulp-open');
var collect = require('gulp-collect');

console.log('Imports finished.');

gulp.task('watch', function() {

  return gulp //
    .watch('MyTestProject/App/**/*.spec.js', handleFileChanged)
    .on('error', handleGulpError);

  function handleFileChanged(vinyl) {

    var pathForChangedFile = './' + vinyl.replace(/\\/g, '/');

    var karmaOptions = {
      configFile: __dirname + '/karma.conf.js',
      action: 'start',
      browsers: ['PhantomJS'],
      singleRun: true,
      files: firstKarmaFiles //
        .concat([
          {
            pattern: pathForChangedFile,
            included: false
          }
        ]) //
        .concat(lastKarmaFiles)
    };

    var karmaServer = new KarmaServerConstructor(karmaOptions);
    karmaServer.start();
    }    
});
于 2017-11-13T16:38:01.950 回答