【问题标题】:How to setup unit testing for multiple require module如何为多个 require 模块设置单元测试
【发布时间】:2015-05-20 12:03:54
【问题描述】:

我正在开发一个代码库,其中代码分为多个 require 模块。 即每个部分都有自己的 main.js 和 require 配置。

我想使用 Karma 为整个代码库设置代码覆盖率。

由于每个部分都有自己的 requirejs 配置,我为每个模块创建了一个 test-main.js。

并让 karma.config 加载所有 test-main.js 文件。

我遇到了问题。 baseUrl 之间存在冲突。

当只测试一个模块时,它工作正常。

有什么想法吗?

【问题讨论】:

  • 如果你用不同的配置链接多个 karma runner 会发生什么?
  • 这是个好建议。但是有可能链接多个业力跑步者吗?我用谷歌搜索了它,但没有运气。

标签: javascript unit-testing requirejs karma-runner karma-coverage


【解决方案1】:

您可以通过编程方式使用 karma 链接多个 karma runner。 Karma offers an API 使其以编程方式工作(恕我直言,这不是很清楚 - 一些示例可以改进它)。

首先,您需要一组配置,然后您需要以编程方式调用 karma 并将调用链接起来。

创建配置数组

function getConfiguration(filename){
  return {
    // you can enrich the template file here if you want
    // e.g. add some preprocessor based on the configuration, etc..
    //
    // if not set in the config template 
    // make Karma server launch the runner as well
    singleRun: true,
    // point to the template
    configFile : __dirname + filename
  };
}

function createConfigurations(){
    return [getConfiguration('conf1.js'), getConfiguration('conf2.js'), etc...];
}

以编程方式启动 Karma

function startKarma(conf, next){
  karma.server.start(conf, function(exitCode){
    // exit code === 0 is OK
    if (!exitCode) {
      console.log('\tTests ran successfully.\n');
      // rerun with a different configuration
      next();
    } else {
      // just exit with the error code
      next(exitCode);
  }
});

连锁业力跑步者

// Use the async library to make things cleaner
var async = require('async');
var karma = require('karma');

var confs = createConfigurations();
async.eachSeries(confs, startKarma, function(err){
  if(err){
    console.log('Something went wrong in a runner');
  else {
    console.log('Yay! All the runners have finished ok');
  }
});

【讨论】:

  • 好的。我刚刚尝试直接从 (karma-runner.github.io/0.12/dev/public-api.html) 运行这段代码,它会启动服务器,但不会启动浏览器并运行测试。我错过了什么? var server = require('karma').server; server.start({port: 9876}, function(exitCode) { console.log('Karma has exited with ' + exitCode); process.exit(exitCode); });
  • 我试过你的方法。它只调用数组中的第一个配置并停止。
  • 我更新了代码,让 runner 被 karma 和 require 触发。停止前在控制台打印什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-25
  • 1970-01-01
  • 2015-05-10
  • 2014-03-02
  • 1970-01-01
相关资源
最近更新 更多