Using JSCoverage with the MochiKit test suite

This is the second in a series of articles about using the JSCoverage tool with various JavaScript testing frameworks. Yesterday we discussed using JSCoverage with the script.aculo.us test suite; today we will cover (no pun intended) the MochiKit test suite.

To run the test suite, first we have to download the latest version of MochiKit (currently version 1.4.2). We will assume this distribution is unpacked in the F:\ root directory, creating the directory F:\MochiKit-1.4.2. Then we can open the URL file:///F:/MochiKit-1.4.2/tests/index.html in a web browser:

The MochiKit test suite

To get code coverage for this test suite, we first instrument the code using the jscoverage program:

jscoverage F:\MochiKit-1.4.2 F:\instrumented-mochikit

Then we open the URL file:///F:/instrumented-mochikit/jscoverage.html?tests/index.html in our web browser:

The MochiKit test suite, instrumented using JSCoverage

The code coverage statistics are displayed in the “Summary” tab:

The JSCoverage "Summary" tab

That works, but there is one improvement we can make. Notice that the list of files in the “Summary” tab contains both the files in the SUT (in the lib directory) and the files in the test suite itself (in the tests directory). We are not really interested in coverage statistics for the latter; we can tell the jscoverage program to avoid instrumenting these files by running it with the --no-instrument option:

jscoverage --no-instrument=tests F:\MochiKit-1.4.2 F:\instrumented-mochikit

With that command, jscoverage will not instrument any JavaScript files in the tests directory. We can run the test suite again in our web browser and see that the files in the tests directory are no longer displayed:

The JSCoverage "Summary" tab (after using --no-instrument)

Tomorrow, we will look at using JSCoverage with the jQuery test suite.