无法在捆绑的aurelia应用程序的单元测试中使用帮助程序类.RequireJS配置?

Chr*_*ini 6 requirejs aurelia aurelia-cli

摘要

使用aurelia cli和包含的默认任务,我无法利用单元测试中位于test文件夹中的辅助类.

细节

从使用au new创建的示例应用程序开始,我在'test/util/helper.ts'中有一个设计的帮助器类:

export class Helper {
    Property : string;
}
Run Code Online (Sandbox Code Playgroud)

此类由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!');
  });
});
Run Code Online (Sandbox Code Playgroud)

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

  • 将typescript编译器的源更改为包含测试文件夹下的文件

    "transpiler": {
       "id": "typescript",
       "displayName": "TypeScript",
       "fileExtension": ".ts",
       "dtsSource": [
         "./typings/**/*.d.ts",
         "./custom_typings/**/*.d.ts"
       ],
       "source": ["src\\**\\*.ts","test\\**\\*.ts"]
    },
    
    Run Code Online (Sandbox Code Playgroud)
  • 修改app-bundle以从测试文件夹中排除任何文件

      {
        "name": "app-bundle.js",
        "source": {
          "include": [
            "[**/*.js]",
            "**/*.{css,html}"
          ],
          "exclude": [
            "**/test/**/*"
          ]
        }
      },
    
    Run Code Online (Sandbox Code Playgroud)
  • 添加一个新的包(test-util-bundle),其中包含来自test\util文件夹的文件,并排除src和test/unit文件夹中的文件

    {
      "name": "test-util-bundle.js",
      "source": {
        "include": [
          "[**/*.js]"
        ],
        "exclude": [
          "**/src/**/*",
          "**/test/unit/**/*"
        ]
      }
    },
    
    Run Code Online (Sandbox Code Playgroud)

将应用程序与'au build'捆绑在一起后,我有三个bundle(app/vendor/test-util),test-util-bundle.js包定义了这样的helper类:

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

我怀疑这是问题的根源,但不熟悉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
Run Code Online (Sandbox Code Playgroud)

注: 如果我移动SRC树下helper.ts文件(如做这工作得很好这里).如果您想查看行为,可以在此处使用.

方法#2 - 没有捆绑实用程序类

  • 修改karma.conf.js
    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]
    },
Run Code Online (Sandbox Code Playgroud)

通过此修改(没有实用程序类的捆绑),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
Run Code Online (Sandbox Code Playgroud)

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

Chr*_*ini 3

在 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('/')
}
Run Code Online (Sandbox Code Playgroud)