javascript 大型 JS 应用程序测试 - 避免多个 karma.conf.js 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20034256/
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
Big JS app testing - avoiding multiple karma.conf.js files
提问by lukas.pukenis
I use karma+ jasmine+ phantomfor my headless Javascript tests.
我使用karma+ jasmine+ phantom进行无头 Javascript 测试。
The problem I have is that I have a really big app consisting of a lot of JS modules which I want to test. So I need custom mocks for each case and custom includes for each case.
我的问题是我有一个非常大的应用程序,其中包含许多我想要测试的 JS 模块。所以我需要为每个案例自定义模拟,并为每个案例自定义包含。
karma.conf.js
allows me only to have files
array which consist of patterns for all the files to include which is GREAT if my app would be small and not a big app with ton of files and modules.
karma.conf.js
只允许我拥有files
包含所有文件的模式的数组,如果我的应用程序很小而不是包含大量文件和模块的大型应用程序,那就太好了。
My solutionfor now - create multiple karma.conf.js
files for each test case. But this really sucks as having so lot of config files is a big bloat and if I would want to change one setting(like autoWatch) I would need to change all the config files.
我现在的解决方案-karma.conf.js
为每个测试用例创建多个文件。但这真的很糟糕,因为有这么多配置文件是一个很大的膨胀,如果我想更改一个设置(如autoWatch),我需要更改所有配置文件。
My other solution- write custom handler in front of karma.conf.js
to handle additional parameters(spec file or folder to bypass karma for searching it's config file) and simply build files
array dynamically.
我的另一个解决方案- 在前面编写自定义处理程序karma.conf.js
以处理其他参数(规范文件或文件夹以绕过 karma 以搜索其配置文件)并简单files
地动态构建数组。
Now the problem I see with this is that karma runs only once and I would be limited to run one test spec... and I DO NOT WANT TO MODIFY KARMA ITSELF.
现在我看到的问题是 karma 只运行一次,我只能运行一个测试规范......而且我不想修改 KARMA 本身。
I have also considered using Gruntbut haven't found a way to make it work for multiple test cases.
我也考虑过使用Grunt,但还没有找到让它适用于多个测试用例的方法。
By the way, my ideal structure would be like this:
顺便说一下,我的理想结构是这样的:
to have files:
要有文件:
test/specs/category/unit1_spec.js
test/mocks/category/unit1_mock.js
config file:
配置文件:
files: [
{
'includes': [array_of_includes],
'spec': 'spec_file'
}
]
mock file would be grabbed automatically from appropriate mocks directory.
模拟文件将自动从适当的模拟目录中获取。
and I could do karma start test/specs/category
and it would recursively run all the test cases inside the folder.
我可以这样做karma start test/specs/category
,它会递归地运行文件夹中的所有测试用例。
tl;dr - I want to test comfortably a big app.
tl;dr - 我想轻松地测试一个大应用程序。
I would appreciate anysuggestion to handle this task.
我将不胜感激任何处理此任务的建议。
采纳答案by Eitan Peer
grunt-karmasounds ideal for your needs.
grunt-karma听起来很适合您的需求。
It is a grunt multitask which allows sub tasks to have a common configuration, which can be overridden by specific sub tasks.
它是一个 grunt 多任务,它允许子任务有一个通用的配置,可以被特定的子任务覆盖。
The plugin consolidates Karma configuration into a single file. For example:
该插件将 Karma 配置整合到一个文件中。例如:
karma: {
options: {
configFile: 'karma.conf.js',
runnerPort: 9999,
browsers: ['Chrome', 'Firefox']
},
category1: {
files: ['app/js/category1/**/*.js', 'test/category1/*.js']
},
category2: {
files: ['app/js/category2/**/*.js', 'test/category2/*.js']
}
}
回答by syrnick
Use an ENV variable to pass the argument to files in karma.conf.js:
使用 ENV 变量将参数传递给 karma.conf.js 中的文件:
files: [
include1, include2, ..., includeN,
process.env.JS_TESTS + "/*.js"
]
Then run karma like so:
然后像这样运行业力:
JS_TESTS=test/category2 karma start karma.conf.js
回答by TDDdev
You can use require statements in karma config files.
您可以在 karma 配置文件中使用 require 语句。
for example, in your karma config file you could do:
例如,在您的 karma 配置文件中,您可以执行以下操作:
files: require('./karma.conf.files')
For a fuller answer .. I found another solution from this link: https://groups.google.com/forum/#!topic/karma-users/uAf7TuVBmCQ
对于更完整的答案..我从这个链接找到了另一个解决方案:https: //groups.google.com/forum/#!topic/karma-users/uAf7TuVBmCQ
With karma 0.8 (current stable)
具有 karma 0.8(当前稳定)
// shared_conf.js
module.exports = {
port: 8080
};
// karma1.conf.js
var shared = require('./shared_conf.js');
port = shared.port;
With karma 0.9 (currently in canary release):
使用 karma 0.9(目前在金丝雀版本中):
// shared_conf.js
module.exports = function(karma) {
karma.configure({
port: 8080
});
};
// karma1.conf.js
var shared = require('./shared_conf.js');
module.exports = function(karma) {
shared(karma);
karma.configure({
// override
});
};
This worked for me to pull in an array of file names from a separate config file.
这对我有用,可以从单独的配置文件中提取一组文件名。
回答by Alexander Shutau
I noticed that whatever argument you pass into command line you get this argument camelCased in initial config object.
我注意到无论你传递给命令行的什么参数,你都会在初始配置对象中得到这个参数camelCased。
> karma start i-wanna-debug
module.exports = function (config) {
config.set({
singleRun: config.iWannaDebug ? false : true
});
};
This allows you to use single Karma config file with multiple configurations.
这允许您使用具有多个配置的单个 Karma 配置文件。