大JS应用程序测试 - 避免多个karma.conf.js文件 [英] Big JS app testing - avoiding multiple karma.conf.js files

查看:181
本文介绍了大JS应用程序测试 - 避免多个karma.conf.js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的无头Javascript测试使用 karma + jasmine + phantom

我遇到的问题是我有一个非常大的应用程序,包含很多我想测试的JS模块。 因此,我需要针对每种情况的自定义模拟以及针对每种情况的自定义包含。
$ b karma.conf.js code>允许我只有文件数组,其中包含所有要包含的文件的模式,如果我的应用程序很小而不是一个大应用程序吨的文件和模块。



我的解决方案现在 - 创建多个 karma.conf.js 文件。但是这真的很糟糕,因为拥有大量的配置文件是一个巨大的膨胀,如果我想改变一个设置(如 autoWatch ),我需要更改所有配置文件。



我的其他解决方案 - 在 karma.conf.js 前面编写自定义处理程序来处理其他参数spec文件或文件夹以绕过搜索其配置文件的业力),并简单地动态构建 files 数组。



我看到的问题是业力只运行一次,我只能运行一个测试规范...而且我不想修改KARMA自身



我也考虑过使用 Grunt ,但还没有找到让它适用于多个测试用例的方法。



顺便说一句,我的理想结构是这样的:



拥有文件:

  test / specs / category / unit1_spec.js 
test / mocks / category / unit1_mock.js

配置文件:

 文件:[
{
'includes':[array_of_includes],
'spec':'spec_file'
}
]

模拟文件将从适当的mocks目录自动抓取。



我可以做 karma start test / specs / category ,它会递归地运行该文件夹内的所有测试用例。



tl; dr - I想要轻松测试一个大应用程序。

我很感激任何处理此任务的建议。

解决方案

grunt-karma 听起来非常适合您的需求。



这是一个多任务处理,它允许子任务具有一个通用配置,可以被特定的子任务覆盖。



该插件将Karma配置整合到单个文件中。
例如:

  karma:{
options:{
configFile:'karma。 conf.js',
runnerPort:9999,
browsersers:['Chrome','Firefox']
},
category1:{
files:['app /js/category1/**/*.js','test / category1 / *。js']
},
category2:{
files:['app / js / category2 / ** / *。js','test / category2 / *。js']
}
}


I use karma + jasmine + phantom for my headless Javascript tests.

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.

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.

My solution for 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.

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.

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.

I have also considered using Grunt but haven't found a way to make it work for multiple test cases.

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.

tl;dr - I want to test comfortably a big app.

I would appreciate any suggestion to handle this task.

解决方案

grunt-karma sounds ideal for your needs.

It is a grunt multitask which allows sub tasks to have a common configuration, which can be overridden by specific sub tasks.

The plugin consolidates Karma configuration into a single file. For example:

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']
  }
}

这篇关于大JS应用程序测试 - 避免多个karma.conf.js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆