Silicon Forks Home
Blog
JSCoverage
NRuler
Documentation
Forum

Silicon Forks

Archive for the ‘jQuery’ Category

Using JSCoverage with the jQuery test suite

Wednesday, April 28th, 2010

This is the third in a series of articles about using the JSCoverage tool with various JavaScript testing frameworks:

  1. Using JSCoverage with the script.aculo.us test suite
  2. Using JSCoverage with the MochiKit test suite
  3. Using JSCoverage with the jQuery test suite (this article)

To run the jQuery test suite, we need to check out the jQuery Git repository. We will use Cygwin Git and check out the code into the F:\ directory:

cd /cygdrive/f
git clone git://github.com/jquery/jquery.git
make

The make command retrieves some external dependencies (one of which is QUnit, the framework used in the test suite) and builds the jquery.js file. To run the test suite, we just open the file test/index.html in a web browser:

The jQuery test suite

We will instrument the code using the jscoverage program, using the --no-instrument option to avoid instrumenting the test suite directory itself (as we did for the MochiKit test suite):

jscoverage --no-instrument=test F:\jquery F:\instrumented-jquery

Unfortunately, things do not go smoothly; we get the following output:

jscoverage:src/intro.js:18: SyntaxError: missing } after function body
jscoverage: parse error in file src/intro.js

The contents of the file F:\jquery\src\intro.js are as follows:

/*!
 * jQuery JavaScript Library v@VERSION
 * http://jquery.com/
 *
 * Copyright 2010, John Resig
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * Includes Sizzle.js
 * http://sizzlejs.com/
 * Copyright 2010, The Dojo Foundation
 * Released under the MIT, BSD, and GPL Licenses.
 *
 * Date: 
 */
(function( window, undefined ) {

That is the entire file. The file is not actually a valid JavaScript file by itself (which is why jscoverage was confused by it) but is used as input to the build process used to construct the final jquery.js file. Since this is needed only for building the code, not for running it, we can skip it entirely when instrumenting the code. We tell jscoverage to do this using the --exclude option:

jscoverage --no-instrument=test --exclude=src/intro.js F:\jquery F:\instrumented-jquery

Running this command, the previous error is gone, but we get a new error:

jscoverage:src/outro.js:1: SyntaxError: syntax error
jscoverage: parse error in file src/outro.js

Looking at F:\jquery\src\outro.js, we see again this is just a fragment of a JavaScript file, not a valid JavaScript file by itself:

})(window);

We will add another --exclude option for this file. (Note that the --exclude option can be used multiple times.)

jscoverage --no-instrument=test --exclude=src/intro.js --exclude=src/outro.js F:\jquery F:\instrumented-jquery

Finally, the command runs without any errors. We can run the instrumented test suite by opening the URL file:///F:/instrumented-jquery/jscoverage.html?test/index.html in our web browser:

The jQuery test suite, instrumented using JSCoverage

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