Javascript – How to handle measuring Code Coverage in JavaScript

javascripttest-coverageunit testing

In order to measure Code Coverage for JavaScript unit tests, one needs to instrument the code, run the tests and then perform post-processing.

My concern is that, as a result, you are unit testing code that will never be run in production. Since JavaScript isn't compiled, what you test should be precisely what you execute.

So here's my question, how do you handle this? One thought I had was to run Unit Testing on the production code and use that for my pass fail. I would then create a shadow of my production code, with instrumentation and run my unit tests again; this would give me my code coverage stats.

Has anyone come across a method that is a little more graceful than this?

EDIT
I don't want to use browser plugins, because I then need to use a browser in order to run my unit tests.

Best Answer

Read this on code-coverage tools for JavaScript, which has some links, though most are indeed browser extensions.

However, do consider JsTestDriver, which has code coverage support and allows for testing outside of the browser by connecting with another test tool (like HtmlUnit).