Search code examples
javascriptvisual-studio-2013requirejsamdchutzpah

Visual Studio Chutzpah Running test on different projects with AMD modules


I have two projects under a solution, one is my main web project, say MyProject and the other serves for testing purposes, say MyProject.Tests.

Solution
    MyProject
    MyProject.Tests

I want to have my JavaScript headless tests running to the second one. On the first project, all the javascript files are under the Scripts directory, like so:

Scripts/
    Common.js
    Libs/
        jquery/
            jquery.js
        requirejs/
            require.js

At the test project, I have my chutzpah.json file on root.

MyProject.Tests
    chutzpah.json
    Tests/
        Specs/
            spec.js

The file has this configuration:

{
    "Framework": "jasmine",
    "TestHarnessReferenceMode": "AMD",
    "TestHarnessLocationMode": "SettingsFileAdjacent",
    "Tests": [ { "Path": "Tests/Specs" } ],
    "AMDBasePath": "../MyProject/Scripts",
    "CodeCoverageExcludes": ["*Common.js"],
    "References": [
        { "Path": "../MyProject/Scripts/Libs/requirejs/require.js" },
        { "Path": "../MyProject/Scripts/Common.js" }
    ]
}

But when I try to run the spec file I get an error.

Spec file:

define(["jquery"], function ($) {
    //code here. Doesn't matter, the error is because of the jquery module
});

The error, is this:

Error: Error opening C:/Users/g.dyrrahitis/Documents/Visual Studio 2013/Projects/MySolution/MyProject.Tests/Scripts/Libs/jquery/jquery.js: The system cannot find the path specified.

The thing is that chutzpah tries to find my jquery module at the test project rather the main project, where it resides.

Why I'm getting this kind of behavior and how can I solve this please? I've been trying for hours to tackle this with no luck so far.

Note

*The names MySolution, MyProject, MyProject.Tests are used for clarity, rather than using the real names.


Solution

  • I've found it, the chutzpah file hadn't the right configuration options (as expected) for the test harness directory. I needed the TestHarnessDirectory and TestHarnessLocationMode options to explicitly instruct it to look at my main project directory.

    This now is the correct one:

    {
        "TestHarnessDirectory": "../MyProject",
        "TestHarnessLocationMode": "Custom",
        "TestHarnessReferenceMode": "AMD",
    
        "Framework": "jasmine",
        "Tests": [ { "Path": "JavaScript/Specs" } ],
        "AMDBasePath": "../MyProject/Scripts",
        "CodeCoverageExcludes": [ "*Common.js" ],
        "References": [
            { "Path": "../MyProject/Scripts/Libs/requirejs/require.js" },
            { "Path": "../MyProject/Scripts/Common.js" }
        ]
    }
    

    Just needed to tell chutzpah that the harness location mode is custom, in order to provide a directory for it, which is the root of my main project.

    Beware for the right configuration paths then, you may end up struggling for hours like me to find a solution. And read the documentation thoroughly (which I hadn't done).