无法在捆绑的 aurelia 应用的单元测试中使用助手 类。需要 JS 配置?

Unable to use helper classes within unit tests of a bundled aurelia app. RequireJS Configuration?

摘要

使用 aurelia cli 和包含的默认任务,我无法利用单元测试中测试文件夹中的助手 classes。

详情

从使用 au new 创建的示例应用程序开始,我有一个人为的助手 class 位于 'test/util/helper.ts':

export class Helper {
    Property : string;
}

此 class 由 test/unit/app.spec.ts 文件导入:

import {App} from '../../src/app';
import {Helper} from "../util/helper";

describe('the app', () => {
  it('says hello', () => {
    let h = new Helper();
    h.Property = "Testing";
    expect(h.Property).toBe("Testing");
    expect(new App().message).toBe('Hello World!');
  });
});

方法 #1 - 捆绑 我在几个地方修改了 aurelia.json 文件:

将应用程序与 'au build' 捆绑后,我有三个捆绑包 (app/vendor/test-util),其中 test-util-bundle.js 捆绑包定义了这样的助手 class :

define('../test/util/helper',["require", "exports"], function (require, exports) {
    "use strict";
    var Helper = (function () {
        function Helper() {
        }
        return Helper;
    }());
    exports.Helper = Helper;
});

我怀疑这是问题的根源,但对 RequireJS 不是很熟悉。

当我执行 运行 'au test' 时,测试失败并出现以下错误:

11 10 2016 12:05:24.606:DEBUG [middleware:source-files]: Fetching C:/git/aurelia-cli-testing/test/test/util/helper
11 10 2016 12:05:24.608:WARN [web-server]: 404: /base/test/test/util/helper
Chrome 53.0.2785 (Windows 7 0.0.0) ERROR
Uncaught Error: Script error for "C:/git/aurelia-cli-testing/test/test/util/helper", needed by: C:/git/aurelia-cli-testing/test/util/helper
http://requirejs.org/docs/errors.html#scripterror
at C:/git/aurelia-cli-testing/scripts/vendor-bundle.js:3763

注: 如果我将 helper.ts 文件移动到 src 树下,这就可以正常工作(如果你想看到行为,就像 here). This is all available here 所做的那样。

方法 #2 - 不捆绑实用程序 class

    let testSrc = [
      { pattern: project.unitTestRunner.source, included: false },
      { pattern: "test/util/**/*.ts", included: false },
      'test/aurelia-karma.js'
    ];

    ...

    preprocessors: {
      [project.unitTestRunner.source]: [project.transpiler.id],
      ["test/util/**/*.ts"]: [project.transpiler.id]
    },

通过此修改(未捆绑实用程序 class)karma 产生以下错误:

18 10 2016 16:56:59.151:DEBUG [middleware:source-files]: Fetching C:/git/aurelia-cli-testing/test/util/helper
18 10 2016 16:56:59.152:WARN [web-server]: 404: /base/test/util/helper
Chrome 53.0.2785 (Windows 7 0.0.0) ERROR
  Uncaught Error: Script error for "C:/git/aurelia-cli-testing/test/util/helper", needed by: C:/git/aurelia-cli-testing/test/unit/app.spec.js
  http://requirejs.org/docs/errors.html#scripterror
  at C:/git/aurelia-cli-testing/scripts/vendor-bundle.js:3763

感谢阅读,如有任何帮助将不胜感激!

我必须执行以下操作: 1. 更新 aurelia-project/aurelia.json 文件。添加此

  "unitTestRunnerUtils": {
"id": "karmaUtils",
"displayName": "Karma",
"source": "test\utils\**\*.js"   },

然后在karma.conf.js文件中更新了这两个地方。

let testSrc = [ { pattern: project.unitTestRunner.source, included: false }, { pattern: project.unitTestRunnerUtils.source, included: false}, 'test/aurelia-karma.js' ];

preprocessors: { [project.unitTestRunner.source]: [project.transpiler.id], [project.unitTestRunnerUtils.source]: [project.transpiler.id] },

然后成功了...

这是 github 上的示例项目。

https://github.com/duranmg/demo-aurelia-testing

在 Aurelia 团队成员的帮助下,对随 aurelia cli 分发的 aurelia-karma.js 文件进行了小幅修改,解决了该问题:

应修改 normalizePath 函数以在适用的地方附加“.js”:

function normalizePath(path) {
  var normalized = []
  var parts = path
    .split('?')[0] // cut off GET params, used by noext requirejs plugin
    .split('/')

  for (var i = 0; i < parts.length; i++) {
    if (parts[i] === '.') {
      continue
    }

    if (parts[i] === '..' && normalized.length && normalized[normalized.length - 1] !== '..') {
      normalized.pop()
      continue
    }

    normalized.push(parts[i])
  }

  //Use case of testing source code. RequireJS doesn't add .js extension to files asked via sibling selector
  //If normalized path doesn't include some type of extension, add the .js to it
  if(normalized.length > 0 && normalized[normalized.length-1].indexOf('.') < 0){
    normalized[normalized.length-1] = normalized[normalized.length-1] + '.js';
  }

  return normalized.join('/')
}