views:

243

answers:

2

I've got a rather hideous and large javascript file that I've inherited from a dev I loathe. There is a lot of dead code, and I find I've spend a lot of time refactoring functions that aren't even called.

Ideally, I just want something that can tie into the js engine and keep track of when and how many times functions are called.

In FF, I can get a list of the functions by walking the window object, and dynamically wrap them all in a method that would log the call to them, and then call the function as normal.

Unfortunately, in IE, I can't use this as I can't seem to find a way to get a list of all the functions that have been loaded. And I can't run this app in FF, as it's horribly browser specific. At last count there were 138 lines containing "new ActiveXObject(...)"

Help, either with a tool that can do this, or at the very least, a way to get a list of the functions that IE7 has loaded from the user script.

Thanks -c

+4  A: 

Try JSCoverage.

JSCoverage is a tool that measures code coverage for JavaScript programs.

JSCoverage works by instrumenting the JavaScript code used in web pages. Code coverage statistics are collected while the instrumented JavaScript code is executed in a web browser.

The instrumentation can be done on-the-fly if you set the JSCoverage Server to run as an HTTP proxy and configure your browser to go through it.

One way to use it is:

  1. Launch JSCoverage Server in proxy mode:
jscoverage-server --proxy --verbose
  1. Configure your browser to you use localhost:8080 as the HTTP proxy.
  2. Add the following bookmarklet, making sure the relative path to jscoverage is correct:
javascript:void(window.open('jscoverage/jscoverage.html'))
  1. Run your tests.
  2. Run the bookmarklet. It will popup a new window that shows you the coverage results.
Ates Goral
I couldn't figure out how to use that with this system. It's full of frames and ajax.
Chad
I'll add more details to my answer.
Ates Goral
Looks like it would work, unfortunately, I can't change my browsers proxy settings...we're pretty locked down here.
Chad
You don't need to use it in proxy mode. First load up jscoverage.html in your browser and then enter the URL to your application in the text field. For that, I think you may need to pre-instrument your JS files using the jscoverage command line tool.
Ates Goral
A: 

There is a Firebug extension for JS Code Coverage...

FirebugCodeCoverage 0.1 (https://addons.mozilla.org/en-US/firefox/addon/4837)

Unfortunately, its not currently updated for the latest version of FF.

Nirmal Patel