{"id":24258,"date":"2012-04-17T06:49:44","date_gmt":"2012-04-17T11:49:44","guid":{"rendered":"https:\/\/blogs.jetbrains.com\/idea\/?p=7179"},"modified":"2012-04-17T06:49:44","modified_gmt":"2012-04-17T11:49:44","slug":"code-coverage-for-javascript-unit-testing","status":"publish","type":"idea","link":"https:\/\/blog.jetbrains.com\/fr\/idea\/2012\/04\/code-coverage-for-javascript-unit-testing","title":{"rendered":"Code Coverage for JavaScript Unit Testing"},"content":{"rendered":"<p><em style=\"padding: 0.5em 0 0; display:block\">This is a cross-post from <a href=\"https:\/\/blog.jetbrains.com\/webide\/2012\/04\/javascript-unit-testing-with-code-coverage\">WebStorm &amp; PhpStorm blog<\/a>, originally posted by Sergey Simonchik.<\/em><\/p>\n<p>Measuring JavaScript code coverage naturally complements unit testing. It provides a clear picture of which parts of your code remain untested, and helps you focus additional tests on the uncovered code.<\/p>\n<p>From now on, users of <a href=\"http:\/\/code.google.com\/p\/js-test-driver\/\" target=\"_blank\" rel=\"noopener\">JsTestDriver<\/a> can measure code coverage visually, right in the editor with JsTestDriver plugin for IntelliJ IDEA. Make sure you have installed <a href=\"http:\/\/confluence.jetbrains.com\/display\/IDEADEV\/IDEA+11.1+EAP\" target=\"_blank\" rel=\"noopener\">11.1.2 EAP<\/a> with the latest version of the <a href=\"http:\/\/plugins.intellij.net\/plugin\/?id=4468\" target=\"_blank\" rel=\"noopener\">plugin<\/a>.<!--more--><\/p>\n<p>To illustrate the case, we have created a <a href=\"http:\/\/confluence.jetbrains.net\/download\/attachments\/41490213\/percentage-sample.zip\" target=\"_blank\" rel=\"noopener\">sample project<\/a> for you.<\/p>\n<p>When you open <em>percentage-test.js<\/em> for the first time, you will see error highlighting on the <code>TestCase<\/code> method call expression. Position the  caret on the error-highlighted code, press <i>Alt+Enter<\/i>\/<i>Option+Enter<\/i> and choose &#8220;Add JsTestDriver assertion framework support&#8221; from the list of available intention actions.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3678\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2012\/04\/webide-add-jstd-support-4.png\" alt=\"\" width=\"623\" height=\"234\" \/><\/p>\n<p>A global JavaScript library named \u201cJsTestDriver Assertion Framework\u201d  will be created\u00a0and associated with a project. To view it, open the  Settings dialog (<i>File | Settings<\/i> or <i>IntelliJ IDEA | Preferences<\/i>) and select <i>JavaScript Libraries<\/i>.<\/p>\n<p>Before you can run any of your tests, you need to start the test server and capture at least one slave browser. The server does not have to   reside on the machine where the test runner is located; similarly, the browsers   themselves may be located on different machines.<\/p>\n<h3>Starting JsTestDriver server locally<\/h3>\n<p>To launch the JsTestDriver server, look for the tool window at the bottom of the screen.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3650\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2012\/04\/webide-jstd-local-server-is-not-running.png\" alt=\"\" width=\"471\" height=\"186\" \/><\/p>\n<p>Click the green arrow to the left of the address bar to start a local server. The status bar turns yellow to let you know the server is running, but has no slave browsers.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3670\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2012\/04\/webide-jstd-local-server-no-captured-browsers-3.png\" alt=\"\" width=\"473\" height=\"186\" \/><\/p>\n<p>Click the browser icon you want to capture. Alternatively, you can copy\u00a0the capture URL and paste it in a browser. The status bar   turns green, and the corresponding browser icon lights up. Now you\u2019re ready to run tests.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3671\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2012\/04\/webide-jstd-local-server-is-ready-2.png\" alt=\"\" width=\"473\" height=\"186\" \/><\/p>\n<h3>Running tests with code coverage<\/h3>\n<p>To run tests, right-click <em>percentage.jstd<\/em> and choose <i>Run &#8216;percentage.jstd&#8217; with Coverage<\/i>. Tests will be run\u00a0against the local JsTestDriver server and you will get\u00a0a tree diagram.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3675\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2012\/04\/webide-test-result-tree-3.png\" alt=\"\" width=\"591\" height=\"228\" \/><\/p>\n<p>It also shows which lines were exercised by your unittests, and which weren&#8217;t, in two places. First, in your source files, it adds a green bar (exercised) or red bar (omitted) to the left of the source lines:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3674\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2012\/04\/webide-editor-covered-lines-2.png\" alt=\"\" width=\"502\" height=\"289\" \/><\/p>\n<p>You can also see test coverage statistics by file and by directory in your Project View:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3662\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2012\/04\/webide-project-view-coverage.png\" alt=\"\" width=\"375\" height=\"154\" \/><\/p>\n<h3>Excluding files from code coverage<\/h3>\n<p>If you use a JavaScript library in your code (e.g. jQuery), you don&#8217;t need to measure code coverage for library code. To exclude a JavaScript file from the coverage, open JsTestDriver Run Configuration you intend to run, select the <i>Coverage<\/i> tab, and add the file to the excluded file paths.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2012\/04\/webide-exclude-files-from-coverage.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3676\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2012\/04\/webide-exclude-files-from-coverage.png\" alt=\"\" width=\"745\" height=\"539\" \/><\/a><\/p>\n<p>We&#8217;d appeciate to hear responses from you in our\u00a0<a href=\"http:\/\/youtrack.jetbrains.com\/issues\/WI#newissue=yes\" target=\"_blank\" rel=\"noopener\">issue tracker<\/a> ;).<\/p>\n<p><em>Test with pleasure!<\/em><\/p>\n","protected":false},"author":63,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","categories":[808],"tags":[197,207],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/idea\/24258"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/idea"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/types\/idea"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/comments?post=24258"}],"version-history":[{"count":0,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/idea\/24258\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/media?parent=24258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/categories?post=24258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/tags?post=24258"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/cross-post-tag?post=24258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}