PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

HTML | 701 lines | 593 code | 108 blank | 0 comment | 0 complexity | b03e0de8593733116082296a53b06de4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
  2. <html>
  3. <head>
  4. <title>JSCoverage user manual</title>
  5. <link rel="stylesheet" type="text/css" href="sh_nedit.min.css">
  6. <link rel="stylesheet" type="text/css" href="doc.css">
  7. <script type="text/javascript" src="sh_main.min.js"></script>
  8. <script type="text/javascript" src="sh_html.min.js"></script>
  9. <script type="text/javascript" src="sh_javascript.min.js"></script>
  10. </head>
  11. <body onload="sh_highlightDocument();">
  12. <h1>JSCoverage user manual</h1>
  13. <p>
  14. JSCoverage is a tool that measures code coverage for JavaScript programs.
  15. </p>
  16. <p>
  17. JSCoverage works by adding instrumentation to JavaScript code before it is
  18. executed in a web browser. JSCoverage provides several alternative ways of doing
  19. this:
  20. </p>
  21. <ul>
  22. <li>The simplest method is to use the <code>jscoverage</code> program to generate
  23. instrumented JavaScript files.
  24. </li>
  25. <li>Alternatively, you can use the <code>jscoverage-server</code> program, a simple web server that instruments
  26. JavaScript code as it is served.
  27. </li>
  28. <li>Finally, <code>jscoverage-server</code> can be run with the <code>--proxy</code> option to
  29. act as a proxy server which instruments any JavaScript code proxied through it.
  30. </li>
  31. </ul>
  32. <p>
  33. The <code>jscoverage-server</code> program (with or without the <code>--proxy</code>
  34. option) has the advantage of being able to store coverage reports to the filesystem.
  35. </p>
  36. <h2>Compiling JSCoverage</h2>
  37. <p>
  38. You can compile JSCoverage on GNU/Linux or Microsoft Windows, using the GCC C++ compiler (<code>g++</code>). On
  39. Windows you will require <a href="">Cygwin</a> or <a
  40. href="">MinGW/MSYS</a>.
  41. </p>
  42. <p>
  43. You can extract and compile the code with the following commands:
  44. </p>
  45. <pre>
  46. tar jxvf jscoverage-0.4.tar.bz2
  47. cd jscoverage-0.4/
  48. ./configure
  49. make
  50. </pre>
  51. <p>
  52. This will create the <code>jscoverage</code> and <code>jscoverage-server</code>
  53. executables (<code>jscoverage.exe</code> and <code>jscoverage-server.exe</code>
  54. on Windows). You can install the executables in <code>/usr/local</code> with the
  55. command:
  56. </p>
  57. <pre>
  58. make install
  59. </pre>
  60. <p>
  61. Alternatively, you may simply copy the <code>jscoverage</code> executable and/or
  62. the <code>jscoverage-server</code> executable to a suitable location in your
  63. <code>PATH</code>.
  64. </p>
  65. <h2>Using the <code>jscoverage</code> program</h2>
  66. <p>
  67. To demonstrate how the <code>jscoverage</code> program works, we will use the
  68. trivial example JavaScript code located in the
  69. <code>doc/example/</code> directory of the JSCoverage distribution. You can run
  70. this example by viewing the file <code>doc/example/index.html</code> in your web browser.
  71. </p>
  72. <p>
  73. Generating code coverage statistics for this example using the
  74. <code>jscoverage</code> program involves the following steps:
  75. </p>
  76. <h3>1. Instrumenting code</h3>
  77. <p>
  78. The first step is to add instrumentation to your JavaScript code. You do this by
  79. executing <code>jscoverage</code> with two arguments:
  80. </p>
  81. <pre>
  82. jscoverage <var>SOURCE-DIRECTORY</var> <var>DESTINATION-DIRECTORY</var>
  83. </pre>
  84. <p>
  85. <var>SOURCE-DIRECTORY</var> is the directory containing the JavaScript code to be instrumented,
  86. and <var>DESTINATION-DIRECTORY</var> is the name of the
  87. directory to which <code>jscoverage</code> should output the instrumented code.
  88. The <code>jscoverage</code> program will create <var>DESTINATION-DIRECTORY</var> if necessary and (recursively) copy
  89. <var>SOURCE-DIRECTORY</var> to <var>DESTINATION-DIRECTORY</var>, instrumenting
  90. any files ending with a <code>.js</code> extension.
  91. </p>
  92. <p>
  93. The directory structure under <var>SOURCE-DIRECTORY</var> is preserved, so that if you have a file
  94. <code><var>SOURCE-DIRECTORY</var>/dir/index.html</code> referencing the script
  95. <code><var>SOURCE-DIRECTORY</var>/dir/script.js</code>, then
  96. <code>jscoverage</code> will create a copy of the HTML file at
  97. <code><var>DESTINATION-DIRECTORY</var>/dir/index.html</code> and an instrumented
  98. version of the script at
  99. <code><var>DESTINATION-DIRECTORY</var>/dir/script.js</code>.
  100. In addition, <code>jscoverage</code> creates a file called <code>jscoverage.html</code>
  101. which is used to execute the instrumented code.
  102. </p>
  103. <table>
  104. <tr>
  105. <td><pre>
  106. <var>SOURCE-DIRECTORY</var>/
  107. dir/
  108. index.html
  109. script.js
  110. </pre></td>
  111. <td class="arrow">&rarr;</td>
  112. <td><pre>
  113. <var>DESTINATION-DIRECTORY</var>/
  114. dir/
  115. index.html
  116. script.js [instrumented]
  117. jscoverage.html
  118. </pre></td>
  119. </tr>
  120. </table>
  121. <p>
  122. For the example code in the <code>doc/example/</code> directory, you can execute the
  123. following command line from the top-level directory of the JSCoverage distribution:
  124. </p>
  125. <pre>
  126. jscoverage doc/example doc/instrumented
  127. </pre>
  128. <p>
  129. This will create the directory <code>doc/instrumented/</code> and place an
  130. instrumented copy of the code from <code>doc/example/</code> in
  131. <code>doc/instrumented/</code>.
  132. </p>
  133. <table>
  134. <tr>
  135. <td><pre>
  136. doc/example/
  137. index.html
  138. script.js
  139. </pre></td>
  140. <td class="arrow">&rarr;</td>
  141. <td><pre>
  142. doc/instrumented/
  143. index.html
  144. script.js [instrumented]
  145. jscoverage.html
  146. </pre></td>
  147. </tr>
  148. </table>
  149. <h3>2. Executing the instrumented code in a web browser</h3>
  150. <p>
  151. Open the generated <code>jscoverage.html</code> file
  152. (<code>doc/instrumented/jscoverage.html</code>) in your web browser.
  153. The page contains a tabbed user interface:
  154. </p>
  155. <ul>
  156. <li>The "Browser" tab is used to display pages with instrumented scripts.
  157. <li>The "Summary" tab is used to display code coverage data.
  158. <li>The "Source" tab is used to display JavaScript code, showing the number of times
  159. each line of code was executed.
  160. <li>The "About" tab displays information about the current version of JSCoverage.
  161. </ul>
  162. <p><img src="screenshot.png" alt="Screenshot"></p>
  163. <p>
  164. The "Browser" tab contains an <code>&lt;iframe&gt;</code>, which is initially empty.
  165. You can load a page into this frame by
  166. entering its URL into the "URL" input field.
  167. You can load any page located in <code><var>DESTINATION-DIRECTORY</var>/</code>
  168. or a subdirectory underneath <code><var>DESTINATION-DIRECTORY</var>/</code>; loading a page
  169. from outside <code><var>DESTINATION-DIRECTORY</var>/</code>, or from a foreign web
  170. server, will give unexpected results.
  171. </p>
  172. <p>
  173. For example, you can load the file <code>doc/instrumented/index.html</code> by typing
  174. <code>index.html</code> in the "URL" input field (relative URLs are acceptable).
  175. </p>
  176. <p>
  177. Alternatively, you can load a page into the <code>&lt;iframe&gt;</code> by
  178. appending the page URL to the query string of the <code>jscoverage.html</code> URL.
  179. For example, appending <code>?index.html</code> to the <code>jscoverage.html</code> URL
  180. will cause the <code>index.html</code> file to be loaded automatically.
  181. </p>
  182. <p><img src="screenshot2.png" alt="Screenshot"></p>
  183. <p>
  184. For this example, the JavaScript does not execute automatically:
  185. you have to select one of the radio buttons to execute the code.
  186. </p>
  187. <p><img src="screenshot3.png" alt="Screenshot"></p>
  188. <h3>3. Generating a coverage report</h3>
  189. <p>
  190. Once the JavaScript code in the page in the "Browser" tab has been executed, click on
  191. the "Summary" tab. This will display the current code coverage statistics.
  192. </p>
  193. <p><img src="screenshot4.png" alt="Screenshot"></p>
  194. <p>
  195. You can click the checkbox to show a list of statements missed during execution.
  196. </p>
  197. <p><img src="screenshot5.png" alt="Screenshot"></p>
  198. <p>
  199. You can click one of the links to get a detailed view of a JavaScript source file.
  200. </p>
  201. <p><img src="screenshot6.png" alt="Screenshot"></p>
  202. <p>
  203. As long as you do not reload the
  204. <code>jscoverage.html</code> page, the coverage report statistics are
  205. cumulative. If you execute more JavaScript in the frame in the "Browser" tab (e.g., by clicking on a link to
  206. another scripted page, or by reloading the frame containing a scripted
  207. page) and switch to the "Summary" tab again,
  208. the coverage report will combine the statistics from the previous report with any newly generated statistics.
  209. Reloading <code>jscoverage.html</code> resets all code coverage statistics to zero.
  210. </p>
  211. <h2>Inverted mode</h2>
  212. <p>
  213. In some situations it may be difficult to execute your code within the
  214. JSCoverage "Browser" tab. For example, the code may assume that it is running in
  215. the top-level browser window, generating errors if it is executed from within a
  216. frame. JSCoverage has an alternative mode of operation, called <dfn>inverted
  217. mode</dfn>, which may be useful in this case.
  218. </p>
  219. <p>
  220. Normally you load <code>jscoverage.html</code> in your web browser, and in its
  221. "Browser" tab you launch your test code. In inverted mode, you do the
  222. opposite: you load your test page directly in your web browser, and from there
  223. you launch JSCoverage. To do this you need to add some code to your test page:
  224. </p>
  225. <pre class="sh_javascript">
  227. </pre>
  228. <p>
  229. The <code>"path/to/jscoverage.html"</code> should be a URL pointing to the
  230. location of the <code>jscoverage.html</code> file (remember, this will be in the
  231. top level of the <var>DESTINATION-DIRECTORY</var> you specified when running
  232. the <code>jscoverage</code> executable).
  233. </p>
  234. <p>
  235. You can place this code wherever you like in your page: for example, you could
  236. attach it to a button:
  237. </p>
  238. <pre class="sh_html">
  239. &lt;button onclick="'path/to/jscoverage.html');"&gt;Coverage report&lt;/button&gt;
  240. </pre>
  241. <p>
  242. Note that you <em>must</em> use a <code></code> call; simply making a
  243. link to <code>jscoverage.html</code> is not sufficient.
  244. </p>
  245. <p>
  246. An example is located in the <code>doc/example-inverted</code> directory.
  247. You can instrument the code with the <code>jscoverage</code> program:
  248. </p>
  249. <pre>
  250. jscoverage doc/example-inverted doc/instrumented-inverted
  251. </pre>
  252. <p>
  253. You can load the page <code>doc/instrumented-inverted/index.html</code>
  254. directly in your web browser.
  255. From this page, you select one of the radio buttons and then click the "Coverage
  256. report" button to launch the JSCoverage report.
  257. </p>
  258. <p>
  259. Another example is located in the <code>doc/example-jsunit</code> directory.
  260. See the <a href="faq.html#jsunit">FAQ</a> for more information.
  261. </p>
  262. <h2><code>jscoverage</code> command line options</h2>
  263. <p>
  264. The <code>jscoverage</code> program accepts the following options:
  265. </p>
  266. <dl>
  267. <dt><code>-h</code>, <code>--help</code>
  268. <dd>Display a brief help message.
  269. <dt><code>-V</code>, <code>--version</code>
  270. <dd>Display the version of the program.
  271. <dt><code>-v</code>, <code>--verbose</code>
  272. <dd>Explain what is being done.
  273. <dt><code>--encoding=<var>ENCODING</var></code>
  274. <dd>Assume that all JavaScript files use the given character encoding. The
  275. default is ISO-8859-1.
  276. <dt><code>--exclude=<var>PATH</var></code>
  277. <dd>The command
  278. <pre>
  279. jscoverage --exclude=<var>PATH</var> <var>SOURCE-DIRECTORY</var> <var>DESTINATION-DIRECTORY</var>
  280. </pre>
  281. copies <var>SOURCE-DIRECTORY</var> to <var>DESTINATION-DIRECTORY</var>
  282. recursively, but does not copy <var>SOURCE-DIRECTORY</var>/<var>PATH</var>.
  283. <var>PATH</var> must be a complete path relative to <var>SOURCE-DIRECTORY</var>.
  284. <var>PATH</var> can be a file or a directory (in which case the directory and
  285. its entire contents are skipped). This option may be given multiple times.
  286. <dt><code>--js-version=<var>VERSION</var></code>
  287. <dd>Use the specified JavaScript version; valid values for <var>VERSION</var>
  288. are <code>1.0</code>, <code>1.1</code>, <code>1.2</code>, ..., <code>1.8</code>,
  289. or <code>ECMAv3</code> (the default).
  290. <dt><code>--no-highlight</code>
  291. <dd>Do not perform syntax highlighting of JavaScript code.
  292. <dt><code>--no-instrument=<var>PATH</var></code>
  293. <dd>The command
  294. <pre>
  295. jscoverage --no-instrument=<var>PATH</var> <var>SOURCE-DIRECTORY</var> <var>DESTINATION-DIRECTORY</var>
  296. </pre>
  297. copies <var>SOURCE-DIRECTORY</var> to <var>DESTINATION-DIRECTORY</var>
  298. recursively, but does not instrument any JavaScript code in
  299. <var>SOURCE-DIRECTORY</var>/<var>PATH</var>. <var>PATH</var> must be a complete
  300. path relative to <var>SOURCE-DIRECTORY</var>. <var>PATH</var> can be a
  301. (JavaScript) file or a directory (in which case any JavaScript files located
  302. anywhere underneath the directory are not instrumented). This option may be
  303. given multiple times.
  304. </dl>
  305. <h2>Query string options</h2>
  306. <p>
  307. When accessing <code>jscoverage.html</code> in a web browser, you may provide a
  308. query string consisting of options separated by ampersand (<code>&amp;</code>)
  309. or semicolon (<code>;</code>). Any option not containing an equals sign
  310. (<code>=</code>) is considered to be a URL which will be loaded in the "Browser"
  311. tab.
  312. </p>
  313. <dl>
  314. <dt><code>u=<var>URL</var></code>, <code>url=<var>URL</var></code>
  315. <dd>Load <var>URL</var> in the "Browser" tab. (This is the same as specifying
  316. an option without an equals sign.)
  317. <dt><code>m=<var>BOOLEAN</var></code>, <code>missing=<var>BOOLEAN</var></code>
  318. <dd>Determines whether to initially display the "Missing" column in the "Summary"
  319. tab. <var>BOOLEAN</var> can be
  320. <code>true</code>, <code>t</code>, <code>yes</code>, <code>y</code>, <code>on</code>, <code>1</code>
  321. (to display the "Missing" column), or
  322. <code>false</code>, <code>f</code>, <code>no</code>, <code>n</code>, <code>off</code>, <code>0</code>
  323. (to hide the "Missing" column). By default, the "Missing" column is not displayed.
  324. </dl>
  325. <h2>Using the <code>jscoverage-server</code> program</h2>
  326. <p>
  327. The <code>jscoverage-server</code> program is a simple web server. You can use
  328. <code>jscoverage-server</code> to serve files from the <code>doc/example/</code>
  329. directory:
  330. </p>
  331. <pre>
  332. cd doc/example
  333. jscoverage-server --verbose
  334. </pre>
  335. <p>
  336. Once the server is running, you can access the JSCoverage web interface by
  337. visiting the URL <code></code>, and you can
  338. load the <code>doc/example/index.html</code> file by entering
  339. <code>index.html</code> in the "URL" input field. (Or you can do this all in
  340. one step by loading the URL
  341. <code></code> in your web
  342. browser.) The
  343. <code>jscoverage-server</code> program automatically instruments any served
  344. JavaScript code, so that code coverage data will be gathered as the code is
  345. executed in your browser.
  346. </p>
  347. <p>
  348. The web interface is slightly different from that generated by the
  349. <code>jscoverage</code> program: it has a new tab named "Store".
  350. To store coverage data, click the "Store" tab.
  351. </p>
  352. <p><img src="screenshot7.png" alt="Screenshot"></p>
  353. <p>
  354. When you click the "Store Report" button, the coverage data will be saved to a directory named <code>jscoverage-report/</code>.
  355. You can view this stored report at any time by opening the file <code>jscoverage-report/jscoverage.html</code> in
  356. your web browser - you don't need the <code>jscoverage-server</code> running to access it.
  357. </p>
  358. <p>
  359. If you use the "Store" tab again to store coverage data, the new data will be merged with
  360. the previous data in the <code>jscoverage-report/</code> directory. This can be useful,
  361. for instance, if you wish to run a set of tests in different browsers and generate an
  362. aggregate report which combines the data for all of them.
  363. </p>
  364. <p>
  365. You can stop the server by running another instance of <code>jscoverage-server</code> with the
  366. <code>--shutdown</code> option:
  367. </p>
  368. <pre>
  369. jscoverage-server --shutdown
  370. </pre>
  371. <h2>Using <code>jscoverage-server --proxy</code></h2>
  372. <p>
  373. To use <code>jscoverage-server</code> as a proxy server, use the <code>--proxy</code> option:
  374. </p>
  375. <pre>
  376. jscoverage-server --verbose --proxy
  377. </pre>
  378. <p>
  379. Configure your browser to use an HTTP proxy with address and port 8080.
  380. You can then generate code coverage data for a web page on the server <code></code>
  381. by accessing the JSCoverage web interface at the special URL <code></code>.
  382. Note that this URL is not provided by the <code></code> server; it is automatically generated
  383. by the proxy server whenever a URL with path <code>/jscoverage.html</code> is requested.
  384. </p>
  385. <h2><code>jscoverage-server</code> command line options</h2>
  386. <dl>
  387. <dt><code>-h</code>, <code>--help</code>
  388. <dd>Display a brief help message.
  389. <dt><code>-V</code>, <code>--version</code>
  390. <dd>Display the version of the program.
  391. <dt><code>-v</code>, <code>--verbose</code>
  392. <dd>Explain what is being done.
  393. <dt><code>--document-root=<var>PATH</var></code>
  394. <dd>Serve web content from the directory given by <var>PATH</var>. The default is
  395. the current directory. This option may not be given with the <code>--proxy</code> option.
  396. <dt><code>--encoding=<var>ENCODING</var></code>
  397. <dd>Assume that all JavaScript files use the given character encoding. The
  398. default is ISO-8859-1. Note that if you use the <code>--proxy</code> option, the
  399. character encoding will be determined from the <code>charset</code> parameter in
  400. the <code>Content-Type</code> HTTP header.
  401. <dt><code>--ip-address=<var>ADDRESS</var></code>
  402. <dd>Run the server on the IP address given by <var>ADDRESS</var>. The default is <code></code>. Specify
  403. <code></code> to use any address.
  404. <dt><code>--js-version=<var>VERSION</var></code>
  405. <dd>Use the specified JavaScript version; valid values for <var>VERSION</var>
  406. are <code>1.0</code>, <code>1.1</code>, <code>1.2</code>, ..., <code>1.8</code>,
  407. or <code>ECMAv3</code> (the default).
  408. <dt><code>--no-highlight</code>
  409. <dd>Do not perform syntax highlighting of JavaScript code.
  410. <dt><code>--no-instrument=<var>URL</var></code>
  411. <dd>Do not instrument JavaScript code from <var>URL</var>. If you are running <code>jscoverage-server</code>
  412. with the <code>--proxy</code> option, <var>URL</var> should be a full URL. For example:
  413. <pre>
  414. jscoverage-server --proxy --no-instrument=
  415. </pre>
  416. Without <code>--proxy</code>, <var>URL</var> should be only the path portion of a URL:
  417. <pre>
  418. jscoverage-server --no-instrument=/scripts/
  419. </pre>
  420. This option may be given multiple times.
  421. <dt><code>--port=<var>PORT</var></code>
  422. <dd>Run the server on the port given by <var>PORT</var>. The default is port 8080.
  423. <dt><code>--proxy</code>
  424. <dd>Run as a proxy server.
  425. <dt><code>--report-dir=<var>PATH</var></code>
  426. <dd>Use the directory given by <var>PATH</var> for storing coverage reports. The default is
  427. <code>jscoverage-report/</code> in the current directory.
  428. <dt><code>--shutdown</code>
  429. <dd>Stop a running instance of the server.
  430. </dl>
  431. <h2>Advanced topics</h2>
  432. <h3>Storing coverage reports programmatically</h3>
  433. <p>
  434. If you are executing a test suite using <code>jscoverage-server</code>, you can
  435. store a coverage report programmatically by having your test suite call the
  436. <code>jscoverage_report</code> function (automatically generated by
  437. <code>jscoverage-server</code>) after all your tests have finished running:
  438. </p>
  439. <pre class="sh_javascript">
  440. if (window.jscoverage_report) {
  441. jscoverage_report();
  442. }
  443. </pre>
  444. <p>
  445. You can specify the name of the directory in which to store the report by
  446. passing the name as a parameter to the <code>jscoverage_report</code> function:
  447. </p>
  448. <pre class="sh_javascript">
  449. if (window.jscoverage_report) {
  450. // determine the directory name based on the browser
  451. var directory;
  452. if (/MSIE/.test(navigator.userAgent)) {
  453. directory = 'IE';
  454. }
  455. else {
  456. directory = 'other';
  457. }
  458. jscoverage_report(directory);
  459. }
  460. </pre>
  461. <p>
  462. This directory will be a subdirectory under the <code>jscoverage-report/</code>
  463. directory (or whatever is specified with the <code>--report-dir</code> option).
  464. Using the above example, the report would be stored to either
  465. <code>jscoverage-report/IE/</code> or <code>jscoverage-report/other/</code>.
  466. </p>
  467. <p>
  468. It is not necessary that your test suite be executed within the
  469. <code>jscoverage.html</code> web interface to store a coverage report. The URL
  470. of the test suite can simply be loaded directly in a web browser.
  471. </p>
  472. <p>
  473. The example in <code>doc/example-jsunit/</code> demonstrates storing coverage
  474. reports programmatically.
  475. </p>
  476. <h3>Ignoring certain lines of code</h3>
  477. <p>
  478. Sometimes you may wish to exclude certain lines of code from coverage
  479. statistics. Some lines of code may be executed only in certain browsers; other
  480. lines should never be executed at all (they may be present only to detect
  481. programming errors). You can use specially formatted comments in your code to
  482. tell JSCoverage to ignore certain lines of code. These lines will not be
  483. included in the JSCoverage "Summary" tab; in the "Source" tab, these lines will
  484. be indicated with the color yellow.
  485. </p>
  486. <p>
  487. These comments take the following form:
  488. </p>
  489. <pre class="sh_javascript">
  490. //#JSCOVERAGE_IF <var>CONDITION</var>
  491. ...
  493. </pre>
  494. <p>
  495. The comment must be formatted exactly as shown: it must be a line comment
  496. starting with <code>//</code>, it must start in the first column, and it must be
  497. followed by <code>#JSCOVERAGE_IF</code> or <code>#JSCOVERAGE_ENDIF</code> in
  498. uppercase letters with no intervening white space.
  499. </p>
  500. <p>
  501. The <var>CONDITION</var> is an ordinary JavaScript expression; if this
  502. expression evaluates to <code>true</code>, then the lines of code between the
  503. <code>//#JSCOVERAGE_IF</code> and <code>//#JSCOVERAGE_ENDIF</code> comments are
  504. included in coverage statistics; otherwise, they are excluded from coverage
  505. statistics.
  506. </p>
  507. <p>
  508. For example:
  509. </p>
  510. <pre class="sh_javascript">
  511. function log(s) {
  512. if (window.console) {
  513. //#JSCOVERAGE_IF window.console
  514. console.log(s);
  516. }
  517. }
  518. </pre>
  519. <p>
  520. You can exclude code from coverage statistics unconditionally by using
  521. <code>#JSCOVERAGE_IF 0</code> or <code>#JSCOVERAGE_IF false</code>:
  522. </p>
  523. <pre class="sh_javascript">
  524. function f(x) {
  525. if (x === null) {
  526. //#JSCOVERAGE_IF 0
  527. throw 'error';
  529. }
  530. ...
  531. </pre>
  532. <p>
  533. There is also a short form, which must appear on the line preceding an
  534. <code>if</code> statement:
  535. </p>
  536. <pre class="sh_javascript">
  538. if (...) {
  539. ...
  540. }
  541. else if (...) {
  542. ...
  543. }
  544. ...
  545. else {
  546. ...
  547. }
  548. </pre>
  549. <p>
  550. In this form, there is no condition on the <code>//#JSCOVERAGE_IF</code> line
  551. and no <code>//#JSCOVERAGE_ENDIF</code>. You use this form to tell JSCoverage
  552. that you expect only one branch of the <code>if</code> statement to be executed;
  553. coverage statistics will not be collected for the other branch(es). For
  554. example:
  555. </p>
  556. <pre class="sh_javascript">
  557. function log(s) {
  559. if (window.console) {
  560. console.log(s);
  561. }
  562. else if (window.opera) {
  563. opera.postError(s);
  564. }
  565. else {
  566. throw 'no logging function available';
  567. }
  568. }
  569. </pre>
  570. <p>
  571. Currently, <code>//#JSCOVERAGE_IF</code> comments are not recorded in stored coverage reports.
  572. </p>
  573. <h2>Caveats</h2>
  574. <ul>
  575. <li>JSCoverage adds instrumentation to JavaScript code, which will slow down execution speed.
  576. Expect instrumented code to take at least twice as much time to run.
  577. <li>JSCoverage currently instruments only <code>.js</code> files; it does not instrument code in <code>&lt;script&gt;</code>
  578. elements in HTML files.
  579. <li>HTML files must use relative URLs to reference scripts. If you use an absolute URL, your page will reference
  580. the original uninstrumented script rather than the instrumented one, and no code coverage data will be collected.
  581. <li>JSCoverage instruments physical lines of code rather than logical JavaScript statements; it works bests with code
  582. that has exactly one statement per line. If you put multiple statements on a line, or split a line across two or more
  583. statements, you may get strange results.
  584. <li>JSCoverage uses frames. Some web pages that use frames may not function properly when run under JSCoverage, especially
  585. those which try to access the top-level frame (<code></code>, <code>target="_top"</code>, etc.).
  586. <li>JSCoverage is distributed without any warranty. See the <a href="license.html">license</a> for more details.
  587. </ul>
  588. <address>
  589. Copyright &copy; 2007, 2008 <a href=""><img src="siliconforks-16x16.png" width="16" height="16" class="icon" alt="Silicon Forks"></a> <a href=""></a><br>
  590. <a href=""></a>
  591. </address>
  592. </body>
  593. </html>