javascriptqunitchutzpah

Chutzpah Unable to Find Reference When Running Coverage


I have written QUnit tests and am running them in Visual Studio 2012 using the Chutzpah Test Adapter for Test Explorer. Tests that require certain external dependencies, such as jQuery or Kendo fail when I am running code coverage, but not when run without code coverage.

Example test file that reproduces the issue for me:

/// <reference path="qunit-2.5.0.js" />
/// <reference path="../Scripts/jquery-2.1.3.js" />
/// <reference path="../Components/componentEnvironment/componentEnvironment.js" />
module("repro.tests");

test("buildUrl - one part returns part", function () {
    var ce = new ComponentEnvironment();

    var output = ce.buildUrl(["http://localhost"]);

    deepEqual(output, "http://localhost");
});

test("jQuery loaded", function () {
    var fixture = $("#qunit-fixture");

    fixture.append("<div id='grandparent' class='parent grandparent'><div id='parent' class='parent child'><div id='child' class='child'></div></div></div>");

    ok(true);
});

In this example, both tests run successfully when I run them from the Test Explorer, but if I choose Analyze Code Coverage instead, only the first test passes (which depends on an external file I wrote, but not jQuery).

Here is my chutzpah.json file:

{
    "CodeCoverageExcludes": ["jquery.*", "kendo.core.*.js"]
}

Here is the error message from the failing test when running coverage:

Test Name:  repro.tests jQuery loaded
Test FullName:  c:\dev\tfs\mke\web applications\internet\companydashboard\dev\companydashboard\companydashboard\tests\test.js::repro.tests::jquery loaded
Test Source:    c:\dev\tfs\mke\web applications\internet\companydashboard\dev\companydashboard\companydashboard\tests\test.js : line 14
Test Outcome:   Failed
Test Duration:  0:00:00.001

Result Message: Died on test #1 global code@file:///c:/dev/tfs/mke/web%20applications/internet/companydashboard/dev/companydashboard/companydashboard/tests/_Chutzpah.3bddafd322dddb16ba3a30cb58251b28.test.html:51:5
appendChild@[native code]: Can't find variable: $ Expected: undefined, Actual: null
Result StackTrace:  at repro.tests jQuery loaded in c:\dev\tfs\mke\web applications\internet\companydashboard\dev\companydashboard\companydashboard\tests\test.js:line 14

Solution

  • In your chutzpah.json file, in the references section when you list those dependencies (like jquery). Make sure you set the attribute "IsTestFrameworkFile: true". That should help.