Javascript Karma:从命令行运行单个测试文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29150998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:55:42  来源:igfitidea点击:

Karma: Running a single test file from command line

javascriptgruntjsgulpkarma-runnerkarma-jasmine

提问by Gon?alo Vieira

So, I've been looking all over for this, found "similar" answers here, but not exactly what I want.

所以,我一直在寻找这个,在这里找到了“类似”的答案,但不完全是我想要的。

Right now if I want to test a single file with karma, I need to do fit(), fdescribe()on the file in question...

现在,如果我想测试与因缘一个单一的文件,我需要做的fit()fdescribe()对有问题的文件...

However, what I do want is to be able to just call karma, with the config file, and direct it to a specific file, so I don't need to modify the file at all, ie:

但是,我想要的是能够使用配置文件调用 karma,并将其定向到特定文件,因此我根本不需要修改该文件,即:

karma run --conf karma.conf.js --file /path/to/specific/test_file.js

karma run --conf karma.conf.js --file /path/to/specific/test_file.js

is it possible to do this? Or with any helper? (using grunt or gulp?)

是否有可能做到这一点?或者有什么帮手?(使用咕噜声或吞咽?)

采纳答案by bvaughn

First you need to start karma server with

首先,您需要使用以下命令启动 karma 服务器

karma start

Then, you can use grep to filter a specific test or describe block:

然后,您可以使用 grep 过滤特定测试或描述块:

karma run -- --grep=testDescriptionFilter

回答by Yuriy Kharchenko

Even though --filesis no longer supported, you can use an env variable to provide a list of files:

即使--files不再受支持,您也可以使用 env 变量来提供文件列表:

// karma.conf.js
function getSpecs(specList) {
  if (specList) {
    return specList.split(',')
  } else {
    return ['**/*_spec.js'] // whatever your default glob is
  }
}

module.exports = function(config) {
  config.set({
    //...
    files: ['app.js'].concat(getSpecs(process.env.KARMA_SPECS))
  });
});

Then in CLI:

然后在 CLI 中:

$ env KARMA_SPECS="spec1.js,spec2.js" karma start karma.conf.js --single-run

回答by Paul Sweatte

This option is no longer supported in recent versions of karma:

最新版本的 karma 不再支持此选项:

see https://github.com/karma-runner/karma/issues/1731#issuecomment-174227054

https://github.com/karma-runner/karma/issues/1731#issuecomment-174227054

The files array can be redefined using the CLI as such:

文件数组可以使用 CLI 重新定义,如下所示:

karma start --files=Array("test/Spec/services/myServiceSpec.js")

or escaped:

或逃脱:

karma start --files=Array\(\"test/Spec/services/myServiceSpec.js\"\)

References

参考