code coverage for JavaScript


Can I use JSCoverage to measure code coverage for a page on http://example.com/?

In order to measure the code coverage of a page on http://example.com/, you must run jscoverage to create a jscoverage.html file on the example.com server. You cannot use http://example.org/jscoverage.html to measure the code coverage of a page located on http://example.com/.

The fundamental reason for this limitation is the Same Origin Policy for untrusted JavaScript.

(In fact, the current version of JSCoverage is slightly more restrictive than this: it requires that the JavaScript being measured reside under the same directory as the jscoverage.html file.)

Should I use the jscoverage program or the jscoverage-server program?

First, note that the jscoverage program and the jscoverage-server program are mutually exclusive alternatives; you must use one or the other, but not both. If you try to use both at the same time you will get strange results.

Which of the two programs you use is largely a matter of personal preference. The jscoverage-server program has some additional features, such as the ability to store coverage reports to disk. However, the jscoverage program is the more mature of the two and is generally easier to use; this program is recommended if you do not need any of the additional features of jscoverage-server.

Why doesn't my test suite run under JSCoverage in Firefox 3?

Firefox 3 introduces new security restrictions on local files. Depending on the way your test suite is organized, this may cause problems for JSCoverage. (You may get the error "uncaught exception: Permission denied to get property Window._$jscoverage".) There are several workarounds:

  • Place your files on a web server instead of loading them from the file system. This is usually the simplest solution.

  • Organize your HTML files in your test suite in a flat directory structure. For example, suppose that you instrument your test suite with this command:

    jscoverage src instrumented

    If all your HTML files are located directly under the src/ directory (i.e., not in a subdirectory of src/), then you should not have any problems using Firefox 3.

  • Set the Firefox preference named security.fileuri.origin_policy to 3.

I'm trying to load my code with the URL file:///C:/foo/bar/jscoverage.html?foo/bar.html and I'm getting all kinds of JavaScript errors.

Internet Explorer 6 seems to have problems with a file: URL that has a query string with a slash in it.

As a workaround:

  • Place your files on a web server instead of loading them from the file system. (http: URLs work fine.)

  • Do not use a query string; enter your URL in the "URL" field in the "Browser" tab.

  • Rearrange your directory structure so that bar.html ends up in the same directory as jscoverage.html; then you can use the URL file:///C:/foo/bar/jscoverage.html?bar.html with no slash in the query string.

  • Use a different browser. (IE 7 seems to work.)

JSCoverage changes my cursor to a busy cursor, and it never changes back!

Certain browsers (e. g., Internet Explorer 6, Opera, Safari) have trouble changing the cursor. Try moving the mouse and your cursor should change back to normal.

See this discussion for more information.

JSCoverage hangs sometimes when rendering the coverage report.

With Internet Explorer 6 (I think I see a pattern here), garbage collection can cause performance problems. (More information here.)

Can JSCoverage be used with JsUnit?

It is necessary to run JSCoverage in inverted mode. You will have to modify JsUnit to launch JSCoverage.

See the directory doc/example-jsunit for an example. It contains a copy of JsUnit version 2.2alpha11, with the file jsunit/app/main-data.html slightly modified to add a button which launches JSCoverage. It also contains a simple unit test file test.html. You can instrument this example as follows:

jscoverage --no-instrument=jsunit doc/example-jsunit doc/instrumented-jsunit

You can then run the test.html file in JsUnit's jsunit/testRunner.html. The simplest way to do this is probably to copy the contents of doc/instrumented-jsunit to the root of a web server and then access the URL

After the test suite has been run, click on the "Coverage report" button to get a coverage report.

Does Internet Explorer work with jscoverage-server --proxy?

Internet Explorer does not use a proxy for URLs with host or localhost. The proxy will be used if you use your machine's actual IP address or host name. For example, if your IP address is, you can access a URL like in Internet Explorer and it will work. (Under Windows, you can get your machine's IP address with the ipconfig command.)

Copyright © 2007, 2008, 2009, 2010 Silicon Forks siliconforks.com