Instrumenter Class
mechanism to instrument code for coverage. It uses the esprima
and
escodegen
libraries for JS parsing and code generation respectively.
Works on node
as well as the browser.
Usage on nodejs
var instrumenter = new require('istanbul').Instrumenter(),
changed = instrumenter.instrumentSync('function meaningOfLife() { return 42; }', 'filename.js');
Usage in a browser
Load esprima.js
, escodegen.js
and instrumenter.js
(this file) using script
tags or other means.
Create an instrumenter object as:
var instrumenter = new Instrumenter(),
changed = instrumenter.instrumentSync('function meaningOfLife() { return 42; }', 'filename.js');
Aside from demonstration purposes, it is unclear why you would want to instrument code in a browser.
Constructor
Instrumenter
-
options
Parameters:
-
options
ObjectOptional. Configuration options.
-
[coverageVariable]
String optionalthe global variable name to use for tracking coverage. Defaults to
__coverage__
-
[embedSource]
Boolean optionalwhether to embed the source code of every file as an array in the file coverage object for that file. Defaults to
false
-
[preserveComments]
Boolean optionalwhether comments should be preserved in the output. Defaults to
false
-
[noCompact]
Boolean optionalemit readable code when set. Defaults to
false
-
[noAutoWrap]
Boolean optionaldo not automatically wrap the source in an anonymous function before covering it. By default, code is wrapped in an anonymous function before it is parsed. This is done because some nodejs libraries have
return
statements outside of a function which is technically invalid Javascript and causes the parser to fail. This construct, however, works correctly in node since module loading is done in the context of an anonymous function.Note that the semantics of the code returned by the instrumenter does not change in any way. The function wrapper is "unwrapped" before the instrumented code is generated.
-
[codeGenerationOptions]
Object optionalan object that is directly passed to the
escodegen
library as configuration for code generation. ThenoCompact
setting is not honored when this option is specified -
[debug]
Boolean optionalassist in debugging. Currently, the only effect of setting this option is a pretty-print of the coverage variable. Defaults to
false
-
[walkDebug]
Boolean optionalassist in debugging of the AST walker used by this class.
-
Item Index
Methods
instrument
-
code
-
filename
-
callback
Callback based instrumentation. Note that this still executes synchronously in the same process tick
and calls back immediately. It only provides the options for callback style error handling as
opposed to a try-catch
style and nothing more. Implemented as a wrapper over instrumentSync
Parameters:
-
code
Stringthe code to be instrumented as a String
-
filename
StringOptional. The name of the file from which the code was read. A temporary filename is generated when not specified. Not specifying a filename is only useful for unit tests and demonstrations of this library.
-
callback
Function(err, instrumentedCode)- the callback function
instrumentASTSync
-
program
-
filename
-
originalCode
synchronous instrumentation method that instruments an AST instead.
Parameters:
-
program
Stringthe AST to be instrumented
-
filename
StringOptional. The name of the file from which the code was read. A temporary filename is generated when not specified. Not specifying a filename is only useful for unit tests and demonstrations of this library.
-
originalCode
Stringthe original code corresponding to the AST, used for embedding the source into the coverage object
instrumentSync
-
code
-
filename
synchronous instrumentation method. Throws when illegal code is passed to it
Parameters:
-
code
Stringthe code to be instrumented as a String
-
filename
StringOptional. The name of the file from which the code was read. A temporary filename is generated when not specified. Not specifying a filename is only useful for unit tests and demonstrations of this library.
lastFileCoverage
()
Object
returns the file coverage object for the code that was instrumented just before calling this method. Note that this represents a "zero-coverage" object which is not even representative of the code being loaded in node or a browser (which would increase the statement counts for mainline code).
Returns:
a "zero-coverage" file coverage object for the code last instrumented by this instrumenter
lastSourceMap
()
Object
returns the source map object for the code that was instrumented just before calling this method.
Returns:
a source map object for the code last instrumented by this instrumenter