使用单元测试的Typescript项目,使用Chutzpah运行测试的问题

时间:2015-08-07 10:34:02

标签: requirejs typescript chutzpah

我在Visual Studio中有一个使用typescript和requirejs的项目,并且在Chutzpah的帮助下运行了QUnit测试。

我项目中的文件夹结构看起来基本上是这样的:

- chutzpah.json
- app/
    - index.html
    - js/
        - config.js
        - main.ts
        - Module.ts
- tests/
    - Test1.ts

到目前为止,我已经配置了requirejs,以便在我的所有导入中,我必须使用 app / js / Module,例如,加载那个。 测试也运行得很好。

现在,我已经更改了我的requirejs配置,在app文件夹中使用了js的baseUrl,所以我只能使用“Module”进行导入。应用程序本身运行正常,但我无法让测试工作。 使用我当前的chutzpah.json,似乎它想要从../tests/加载测试文件,因此对于Test1示例,它会想要加载../ tests / Test1.js

这是我的config.js:

require.config({
    baseUrl: 'js',
    paths: {
        'swfJQ': '../libs/js/swfJQ'
    },
    shim: {
        'swfJQ': {
            "exports": "swfJQ"
        }
    }
});

这是我的chutzpah.json:

{
    "Framework": "qunit",
    "TestHarnessReferenceMode": "AMD",
    "TestHarnessLocationMode": "SettingsFileAdjacent",
    "TypeScriptModuleKind": "AMD",
    "Tests": [ { "Path": "tests", "Includes": [ "*Tests.ts" ] } ],
    "AMDBaseUrl": "js",
    "AMDAppDirectory": "app",
    "References": [
        { "Path": "app/libs/js/jquery.js", "IsTestFrameworkFile":  true },
        { "Path": "app/libs/require.js", "IsTestFrameworkFile":  true },
        { "Path": "app/config.js" },
        { "Path": "require.d.ts", "IncludeInTestHarness": false },
        { "Path": "qunit.d.ts", "IncludeInTestHarness": false },
        { "Path": "tests", "IncludeInTestHarness": false, "Excludes": ["*.html"] },
        { "Path": "app/js", "IncludeInTestHarness": false }
    ],
    "EnableCodeCoverage ": "true",
    "CodeCoverageIncludes": [
        "*app/js/*"
    ],
    "CodeCoverageExcludes": [
        "app/libs/*",
        "tests/*"
    ]
}

这是测试文件之一:

import Replay = require('../app/js/Module');

QUnit.module("Module tests");

test("Module.constructor", function (assert: QUnitAssert) { /* stuff */ }

我认为require('../ app / js / Module')或多或少是罪魁祸首,并将范围设置为../,但如果我写代码,否则typescript编译器将无法编译。 我想这可能是我很容易丢失的东西,但我无法确定它。

也许这不是我的问题,但我的一般的Typescript设置应该是不同的(所以我不必在我的测试文件中使用require('../ app ...')?

1 个答案:

答案 0 :(得分:2)

原来这应该很容易: 我必须将Chutzpah配置中的AMDBaseUrl设置为" app / js",并删除AMDAppDirectory属性。 我不相信我之前没有尝试过。

我的chutzpah.json:

{
    "Framework": "qunit",
    "TestHarnessReferenceMode": "AMD",
    "TestHarnessLocationMode": "SettingsFileAdjacent",
    "TypeScriptModuleKind": "AMD",
    "Tests": [ { "Path": "tests", "Includes": [ "*Tests.ts" ] } ],
    "AMDBasePath": "app/js",
    "References": [
        { "Path": "app/libs/js/jquery.js", "IsTestFrameworkFile":  true },
        { "Path": "app/libs/js/kendo.ui.core.min.js", "IsTestFrameworkFile":  true },
        { "Path": "app/libs/js/mediaelement-and-player.js", "IsTestFrameworkFile":  true },
        { "Path": "app/libs/require.js", "IsTestFrameworkFile":  true },
        { "Path": "app/libs/js/SCORM_API_wrapper.js" },
        { "Path": "require.d.ts", "IncludeInTestHarness": false },
        { "Path": "qunit.d.ts", "IncludeInTestHarness": false },
        { "Path": "tests", "IncludeInTestHarness": false, "Excludes": ["*.html"] },
        { "Path": "app/js", "IncludeInTestHarness": false }
    ],
    "EnableCodeCoverage ": "true",
    "CodeCoverageIncludes": [
        "*app/js/*"
    ],
    "CodeCoverageExcludes": [
        "app/libs/*",
        "tests/*"
    ]
}