Running JavaScript tests with Chutzpah

Chutzpah support QUnit, Jasmine and Mocha unit tests.

Below are examples of a basic setup for QUnit tests:
  • A file (code.js) which contains the code under tests
var mathLib = {
    add5: function (a) {
        return a + 5;
    }
}
  • A file (test.js) which contains your unit tests
test("will add 5 to number", function () {
    var res = mathLib.add5(10)

    equal(res, 15, "should add 5");
});
  • A test harness file (test.html) which references your tests, code, qunit.js, qunit.css and any other dependencies your code might have.
<html>
<head>
    <link rel="stylesheet" href="qunit.css" type="text/css" />
    <script type="text/javascript" src="code.js"></script>
    <script type="text/javascript" src="qunit.js"></script>
    <script type="text/javascript" src="tests.js"></script>
</head>
<body>
    <h1 id="qunit-header">Unit Tests</h1>
    <h2 id="qunit-banner"></h2>
    <h2 id="qunit-userAgent"></h2>
    <ol id="qunit-tests">
    </ol>
    <div id="qunit-fixture">test markup, will be hidden</div>
</body>
</html>

With these files you can simply run the test using Chutzpah from the command line by passing the exe the path to your html file.

.\chutzpah.console.exe test.html

You can also run this from Visual Studio by right click on the test.html file and choosing "Run JS Tests"

Running test without creating the test harness html file

Chutzpah also support running tests without you needing to create the test harness file shown above. In order for this to work you need to list your tests' dependencies using the // <reference markup at the top of your test file.

For example, the test.js file above would become:

/// <reference path="../js/code.js" />

test("will add 5 to number", function () {
    var res = mathLib.add5(10)

    equal(res, 15, "should add 5");
});

With the reference markup in place you can run the Chutzpah on the .js test file directly.
.\chutzpah.console.exe test.js

You can also run this from Visual Studio by right click on the test.js file and choosing "Run JS Tests"

Running multiple tests files at once

A large project will often break up its JavaScript unit tests into multiple files. To make it easier to run all of them Chutzpah supports running on a whole directory tree. When you execute Chutzpah on a folder like

.\chutzpah.console.exe js/testFolder

it will recursively scan for .js files within testFolder and run any files it believes are test files.

Last edited Apr 4 at 9:32 PM by mmanela, version 14