This is the sixth in a series of articles about using the JSCoverage tool with various JavaScript testing frameworks:
- Using JSCoverage with the script.aculo.us test suite
- Using JSCoverage with the MochiKit test suite
- Using JSCoverage with the jQuery test suite
- Using JSCoverage with the MooTools test suite
- Using JSCoverage with the YUI test suite
- Using JSCoverage with the Dojo test suite (this article)
First, we need to download the Dojo SDK, which includes the Dojo test suite. The latest release is currently version 1.4.2. This release unpacks to a directory named dojo-release-1.4.2-src
. Some of the tests in the test suite need to be run under a web server, so we’ll install the distribution in the document root of an Apache web server, running on port 8080. Then, the test suite, which uses a test framework called DOH (the Dojo Objective Harness), can be executed by loading the URL http://127.0.0.1:8080/dojo-release-1.4.2-src/dojo/tests/runTests.html
in a web browser:
To obtain code coverage data for this test suite using JSCoverage, we will need to make one modification to the source code. The DOH test framework does not work well running inside a frame, so we will use the JSCoverage “inverted mode” to run it: we will add a button to the file util/doh/runner.html
which launches the JSCoverage user interface:
<h3 style="margin: 5px 5px 0px 5px; float: left;">D.O.H.: The Dojo Objective Harness</h3> <button style="margin-top: 5px; float: left;" onclick="window.open('../../jscoverage.html');">Coverage Report</button> <img src="small_logo.png" height="40" style="margin: 0px 5px 0px 5px; float: right;">
Then we can run the jscoverage
program to instrument the code. We will use the --no-instrument
option avoid instrumenting the test suite itself. Some of the files in the distribution contain non-ASCII characters encoded using the UTF-8 character encoding, so we will use the --encoding
option to specify this. (Otherwise, we would get “Illegal character” errors.)
jscoverage --encoding=UTF-8 --no-instrument=dojo/tests dojo-release-1.4.2-src instrumented-dojo
Then we can run the instrumented test suite by accessing the URL http://127.0.0.1:8080/instrumented-dojo/dojo/tests/runTests.html
:
Note that some tests failed (indicated by the red sections in the bar at the top). Looking at the DOH “Log” tab, we see a lot of errors like this:
Error: Bundle not found: currency in dojo.cldr , locale=en-us
Looking in the (uninstrumented) dojo/cldr
directory, we see it contains a lot of .js
files; dojo/cldr/nls/en-us/currency.js
is typical:
// generated from ldml/main/*.xml, xpath: ldml/numbers/currencies ({ USD_symbol:"$" })
The .js
files under the dojo/cldr/nls
directory contain valid JavaScript code, but they do not contain arbitrary JavaScript code; Dojo expects every one of these files to contain a single JavaScript object literal. When jscoverage
adds instrumentation to one of these files, it no longer contains a single JavaScript object literal, and this causes errors when the test suite is run. We need to use the --no-instrument
option to ensure that these files are not instrumented:
jscoverage --encoding=UTF-8 --no-instrument=dojo/tests --no-instrument=dojo/cldr/nls dojo-release-1.4.2-src instrumented-dojo
Then we can run the instrumented tests without any errors:
We can click on the “Coverage Report” button to see the code coverage statistics:
Tomorrow we will look at using JSCoverage with the JsUnit test framework.