This is the fifth 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 (this article)
First, we need to download YUI. We will use the latest release in the YUI 2 series, version 2.8.0r4. The YUI test suite requires a web server to run in some browsers, so we will install it in the document root of a local Apache server, running on port 8080. Also, the file yui/tests/common/tests/YUI.html
has a typo that needs to be corrected; we need to change this:
"../../calendar/tests/datemath.html",
to this:
"../../datemath/tests/datemath.html",
Once that is done, we just open the URL http://127.0.0.1:8080/yui/tests/common/tests/YUI.html
in a web browser to run the tests:
To obtain code coverage data, we will have to make one more modification to the file yui/tests/common/tests/YUI.html
. The YUI test framework does not work inside a frame, so we will use JSCoverage “inverted mode” to run the tests. This entails adding a button to the page to launch the jscoverage.html
file:
<div id="out"></div> <button onclick="window.open('../../../jscoverage.html');">Coverage report</button>
Once that addition is made, we can instrument the code:
jscoverage --no-instrument=tests yui instrumented-yui
Note that the YUI distribution is fairly large, so this command may take a while to run. We can speed things up a bit by using the --exclude
option to skip some directories that are not needed:
jscoverage --no-instrument=tests --exclude=as-docs --exclude=docs --exclude=examples yui instrumented-yui
Then we open up the URL http://127.0.0.1:8080/instrumented-yui/tests/common/tests/YUI.html
in our web browser:
We can then click the “Coverage report” button to launch the jscoverage.html
file:
Tomorrow we will look at using JSCoverage with the Dojo test suite.