gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:08:31 32 4
gpt4 key购买 nike

摘要

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

详情

从使用 au new 创建的示例应用程序开始,我在“test/util/helper.ts”中有一个人为的助手类:

export class Helper {
Property : string;
}

这个类由 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 文件:
  • 更改 typescript 编译器的源代码以包含 test 文件夹下的文件
    "transpiler": {
    "id": "typescript",
    "displayName": "TypeScript",
    "fileExtension": ".ts",
    "dtsSource": [
    "./typings/**/*.d.ts",
    "./custom_typings/**/*.d.ts"
    ],
    "source": ["src\\**\\*.ts","test\\**\\*.ts"]
    },
  • 修改 app-bundle 以从测试文件夹中排除任何文件
      {
    "name": "app-bundle.js",
    "source": {
    "include": [
    "[**/*.js]",
    "**/*.{css,html}"
    ],
    "exclude": [
    "**/test/**/*"
    ]
    }
    },
  • 添加一个新包 (test-util-bundle),其中包含 test\util 文件夹中的文件,并排除 src 和 test/unit 文件夹中的文件
    {
    "name": "test-util-bundle.js",
    "source": {
    "include": [
    "[**/*.js]"
    ],
    "exclude": [
    "**/src/**/*",
    "**/test/unit/**/*"
    ]
    }
    },

  • 将应用程序与 'au build' 捆绑在一起后,我得到了三个包(app/vendor/test-util),其中 test-util-bundle.js 包定义了这样的辅助类:
    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 所做的那样),这可以正常工作。这都是可用的 here如果您想查看行为。

    方法#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]
    },

    通过此修改(不捆绑实用程序类),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

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

    最佳答案

    在 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('/')
    }

    关于requirejs - 无法在捆绑的 aurelia 应用程序的单元测试中使用帮助程序类。 RequireJS 配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39987434/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com