【问题标题】:How to handle wallabyjs and karma configuration (with requirejs)如何处理 wallabyjs 和 karma 配置(使用 requirejs)
【发布时间】:2016-02-29 10:53:36
【问题描述】:

在阅读了关于 Wallaby on a build server (CI) 主题的答案和 cmets 之后,我接受了 wallabyjs 目前还没有为 ci 场景做好准备。好的,但我仍然在质疑自己如何处理典型场景,即在客户端使用 wallabyjs,在 ci 系统上使用 karma(或另一个测试运行器)。特别是在使用 requirejs 时。正如here所解释的那样,有一个

test-main.js — 为测试配置 require.js

使用 wallabyjs 这看起来或多或少像

// delaying wallaby automatic start
wallaby.delayStart();

requirejs.config({
  baseUrl: '/src',

  paths: {
    'jquery': '../lib/jquery',
    'underscore': '../lib/underscore'
  },

  shim: {
    'underscore': {
      exports: '_'
    }
  }
});

require(wallaby.tests, function () {
  wallaby.start();
});

使用因果报应here,看起来或多或少像这样

var TEST_REGEXP = /(spec|test)\.js$/i;
var allTestFiles = [];

// Get a list of all the test files to include
Object.keys(window.__karma__.files).forEach(function(file) {
  if (TEST_REGEXP.test(file)) {
    // Normalize paths to RequireJS module names.
    // If you require sub-dependencies of test files to be loaded as-is (requiring file extension)
    // then do not normalize the paths
    var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
    allTestFiles.push(normalizedTestModule);
  }
});

require.config({
  // Karma serves files under /base, which is the basePath from your config file
  baseUrl: '/base/src',

  // example of using a couple path translations (paths), to allow us to refer to different library dependencies, without using relative paths
  paths: {
    'jquery': '../lib/jquery',
    'underscore': '../lib/underscore',
  },

  // example of using a shim, to load non AMD libraries (such as underscore)
  shim: {
    'underscore': {
      exports: '_'
    }
  },

  // dynamically load all test files
  deps: allTestFiles,

  // we have to kickoff jasmine, as it is asynchronous
  callback: window.__karma__.start
});

我必须维护两个文件吗?是否需要某种有条件的构建?有没有人经历过这种情况?

非常感谢。

【问题讨论】:

    标签: karma-runner wallaby.js


    【解决方案1】:

    您可以将这两个文件合并为一个以重用公共部分并添加一些逻辑以根据当前运行器执行某些位。

    window.wallaby && wallaby.delayStart();
    ...
    if (window.__karma__) {
      ...
    }
    ...
    require.config({
      baseUrl: window.__karma__ ? '/base/src' : '/src',
      ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-08
      • 2016-02-26
      • 1970-01-01
      • 2020-02-27
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多