Search code examples
requirejstypescriptchutzpah

Typescript project with Unit tests, problems running tests with Chutzpah


I have a project in Visual Studio that is using typescript and requirejs, and that is running QUnit tests with the help of Chutzpah.

Folderstructure in my project looks basically like this:

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

up until now, I had requirejs configured so that in all my imports, I had to use app/js/Module, for example, to load that one. Tests were also running just fine.

Now, I have changed my requirejs config to use baseUrl of js inside the app folder, so I can use just "Module" for the import. application itself is running fine, but I cannot get the tests to work. With my current chutzpah.json, it seems that it wants to load the test files from ../tests/, so for the Test1 example, it would want to load ../tests/Test1.js

This is my config.js:

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

This is my 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/*"
    ]
}

This is one of the test files:

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

QUnit.module("Module tests");

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

I think the require('../app/js/Module') is more or less the culprit and will set the scope to ../, but if I write the code otherwise, the typescript compiler won't compile. I guess this is probably something very easy I am missing, but I just can't pin it.

Maybe it is not a chutzpah problem I have, but my general Typescript setup should be different (so I don't have to use require('../app ... ') in my test files)?


Solution

  • Turned out this should have been quite easy: I had to set the AMDBaseUrl in the Chutzpah config to "app/js", and removing the AMDAppDirectory property. Can't believe I didn't try that before.

    My 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/*"
        ]
    }