gpt4 book ai didi

karma-runner - 运行 Karma 时检测环境

转载 作者:行者123 更新时间:2023-12-02 00:19:45 24 4
gpt4 key购买 nike

我有两个运行测试的环境(本地和 travic ci)。如果我在本地运行测试,我需要在测试中进行一些调整。

是否可以使用 Karma 来完成此操作,而无需两个单独的配置文件?

最佳答案

您可以以编程方式调用 karma 并向其传递一个配置对象,然后监听回调以关闭服务器:

karma.server.start(config, function (exitCode){

if(exitCode){
console.err('Error in somewhere!');
}
});

配置对象基本上是一个包含一些属性的对象,您可以使用它来丰富您已有的框架配置文件。

假设“path/to/karma.conf.js”中有一个如下所示的配置文件:

// Karma configuration

module.exports = function(config) {
config.set({

// base path, that will be used to resolve files and exclude
basePath: '../',

// frameworks to use
frameworks: ['mocha'],

files: [ ... ].

// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
// choose it before starting Karma


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,

browsers: ['PhantomJS'],

// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,


// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true,

plugins: [
'karma-mocha',
'karma-phantomjs-launcher'
]

});
};

现在我想在开始 karma 之前稍微调整一下:

function enrichConfig(path){
var moreConfig = {
// say you want to overwrite/choose the reporter
reporters: ['progress'],
// put here the path for your skeleton configuration file
configFile: path
};
return moreConfig;
}

var config = enrichConfig('../path/to/karma.conf.js');

目前,我们正在利用这种技术为所有环境生成多种配置。

我想您可以配置 TravisCI 配置文件以将一些参数传递给包装器,以便激活 enrichConfig 函数中的某些特定属性。

更新

如果您想将参数(例如配置文件路径)传递给脚本,只需在参数数组中查找即可找到它。

假设上面的脚本保存在 startKarma.js 文件中,请将代码更改为:

 var args = process.argv;
// the first two arguments are 'node' and 'startKarma.js'
var pathToConfig = args[2];
var config = enrichConfig(pathToConfig);

然后:

$ node startKarma.js ../path/to/karma.conf.js

关于karma-runner - 运行 Karma 时检测环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24276239/

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