{"id":21837,"date":"2011-10-17T12:12:27","date_gmt":"2011-10-17T12:12:27","guid":{"rendered":"https:\/\/blog.jetbrains.com\/webide\/?p=2765"},"modified":"2021-12-16T15:52:44","modified_gmt":"2021-12-16T14:52:44","slug":"javascript-unit-testing-support","status":"publish","type":"webstorm","link":"https:\/\/blog.jetbrains.com\/fr\/webstorm\/2011\/10\/javascript-unit-testing-support","title":{"rendered":"JavaScript Unit Testing Support"},"content":{"rendered":"<p>If you&#8217;re a JavaScript developer, you probably know that the quality and correctness of your application are crucial. Well, consistency and regression testing just got a little less painful. Meet&nbsp;JsTestDriver plugin &#8211; an open-source project that was originally started at Google and is now contributed to by the JetBrains team.<\/p>\n<p>WebStorm 3.0 bundles it.<\/p>\n<p>Users of other JetBrains IDE &#8211; PhpStorm, IntelliJ IDEA, PyCharm, RubyMine, and AppCode &#8211; can install it from the plugin repository following <a href=\"http:\/\/confluence.jetbrains.net\/display\/WI\/Installation+of+JsTestDriver+IntelliJ+plugin\" target=\"_blank\" rel=\"noopener\">these instructions<\/a>.<\/p>\n<p>It allows you to enjoy <a href=\"http:\/\/code.google.com\/p\/js-test-driver\/wiki\/DesignPrinciples\" target=\"_blank\" rel=\"noopener\">all the benefits of JsTestDriver<\/a> right from the comfort of your IDE. This includes the following features:<!--more--><\/p>\n<ul>\n<li>starting and stopping the server;<\/li>\n<li>running and rerunning tests;<\/li>\n<li>filtering and viewing test results, navigation from results to source code;<\/li>\n<li>jumping from JavaScript exception stacktrace to source code;<\/li>\n<li>support for JsTestDriver configuration file: syntax and error highlighting, basic completion, navigation to referenced files;<\/li>\n<li>capturing messages sent to <span style=\"line-height: 100%; font-family: monospace; background-color: #ffffff; padding: 1px; font-size: 12pt; border: 0.01mm solid #000000;\"><span style=\"color: #660e7a; background-color: #ffffff; font-weight: bold;\">console<\/span><span style=\"background-color: #ffffff;\">.<\/span><span style=\"color: #7a7a2b; background-color: #ffffff;\">log<\/span><span style=\"background-color: #ffffff;\">()<\/span><\/span>;<\/li>\n<li>support for Jasmine, QUnit and JsTestDriver built-in assertion frameworks:\n<ul>\n<li>quick-fixes for enabling global symbol reference resolving for each assertion framework (if you have QUnit or Jasmine tests in a project, you will be prompted to install the corresponding <a href=\"http:\/\/code.google.com\/p\/js-test-driver\/wiki\/XUnitCompatibility\" target=\"_blank\" rel=\"noopener\">adapter<\/a>);<\/li>\n<li>contextual code generation actions (Alt+Insert) for&nbsp;creating new tests, setup and teardown methods for each assertion framework;<\/li>\n<li><a href=\"http:\/\/code.google.com\/p\/js-test-driver\/wiki\/HtmlDoc\" target=\"_blank\" rel=\"noopener\">declarative HTML injection<\/a> support for JsTestDriver built-in assertion framework.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>The best way to understand how this works is to see it in action.<\/p>\n<h2>Getting started<\/h2>\n<p>If you aren&#8217;t aware of JsTestDriver concepts, consider visiting&nbsp;<a href=\"http:\/\/code.google.com\/p\/js-test-driver\/wiki\/GettingStarted\" target=\"_blank\" rel=\"noopener\">http:\/\/code.google.com\/p\/js-test-driver\/wiki\/GettingStarted<\/a>.&nbsp;There is also an excellent&nbsp;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/scriptjunkie\/gg655487.aspx\" target=\"_blank\" rel=\"noopener\">tutorial introduction<\/a> to JavaScript unit testing&nbsp;by Christian Johansen.<\/p>\n<h3>Setup project<\/h3>\n<p>We have created a sample project for you.&nbsp;Please download <a href=\"http:\/\/confluence.jetbrains.net\/download\/attachments\/41490213\/greeter-sample.zip\" target=\"_blank\" rel=\"noopener\">greeter-sample.zip<\/a>, extract it and&nbsp;open the project in your IDE.<\/p>\n<p>If you open <em>GreeterTest.js<\/em> for the first time, you will see error highlighting on the&nbsp;<span style=\"font-family: Courier New; font-size: 11pt;\">TestCase<\/span> method call expression. Position the caret on the error-highlighted code, press <strong>Alt+Enter<\/strong>\/<strong>Option+Enter<\/strong> and choose &#8220;Add JsTestDriver assertion framework support&#8221; from the list of available intention actions.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2011\/10\/webide-initial-project-state.png\" alt=\"\"><\/p>\n<p>A global JavaScript library named &#8220;JsTestDriver Assertion Framework&#8221; will be created&nbsp;and associated with a project. To view it, open the Settings dialog (<strong>File | Settings<\/strong> or <strong>IntelliJ IDEA | Preferences<\/strong>) and select <strong>JavaScript Libraries<\/strong>.<\/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, and the browsers themselves can be on different machines as well.<\/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=\"alignnone size-full wp-image-2883\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2011\/10\/webide-server-state-not-running.png\" alt=\"\" width=\"618\" height=\"179\"><\/p>\n<p>Click the green arrow to the right of the address bar to start the 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=\"alignnone size-full wp-image-2884\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2011\/10\/webide-server-state-no-captured-browsers.png\" alt=\"\" width=\"618\" height=\"179\"><\/p>\n<p>Then, copy the capture URL and paste it in a browser. The status bar turns green, and the corresponding browser icon lights up. Now you&#8217;re ready to run tests.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2885\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2011\/10\/webide-server-state-ready.png\" alt=\"\" width=\"617\" height=\"180\"><\/p>\n<h3><span style=\"color: #000000; font-size: 18px; line-height: 27px;\">Running tests<\/span><\/h3>\n<p>To run tests, right-click <em>greeter.jstd<\/em> and choose <strong>Run &#8220;greeter.jstd&#8221;<\/strong>. Tests will be run&nbsp;against the local JsTestDriver server.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2889\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2011\/10\/webide-tests-run-results.png\" alt=\"\" width=\"620\" height=\"203\"><\/p>\n<p>In our case we have one failed test &#8216;GreeterTest.test greet null&#8217;. You can easily navigate from the stack trace to the source code that causes problems. You can also navigate from the test results tree to the test case\/test function declaration using the&nbsp;<strong>F4<\/strong> key.<\/p>\n<h3>Generate code<\/h3>\n<p>In order to remove duplicated code that sets the&nbsp;<span style=\"font-family: Courier New; font-size: 11pt;\">greeter<\/span> local variable, you can refactor the Greeter object&nbsp;creation to&nbsp;a special method called <span style=\"font-family: Courier New; font-size: 11pt;\">setUp<\/span>, which is called before each test function is executed. Just invoke the&nbsp;<em>Generate<\/em> action by pressing <strong>Alt+Insert<\/strong> inside of a <span style=\"font-family: Courier New; font-size: 11pt;\">TestCase<\/span> method call expression, and then choose &#8220;JsTestDriver Setup&#8221;.<\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-2895 aligncenter\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2011\/10\/webide-setup-method-generate-action.png\" alt=\"\" width=\"468\" height=\"211\"><\/p>\n<p>Use the <strong>Shift+F10<\/strong> keyboard shortcut to re-run the tests after changing code.<\/p>\n<p>Test with pleasure!<\/p>\n<p><a href=\"http:\/\/www.jetbrains.net\/confluence\/display\/WI\/Web+IDE+EAP\" target=\"_blank\" rel=\"noopener\">Download WebStorm for your platform from project EAP page<\/a>.<\/p>\n<p>Note: Users of PhpStorm, as well as other JetBrains IDE &#8211; IntelliJ IDEA, PyCharm, RubyMine, and AppCode &#8211; should install it from the plugin repository following <a href=\"http:\/\/confluence.jetbrains.net\/display\/WI\/Installation+of+JsTestDriver+IntelliJ+plugin\" target=\"_blank\" rel=\"noopener\">these instructions<\/a>.<\/p>\n","protected":false},"author":47,"featured_media":0,"comment_status":"open","ping_status":"open","template":"","categories":[601],"tags":[1130,1128,197,1125,743,1129,1131,1126,443],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/webstorm\/21837"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/webstorm"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/types\/webstorm"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/comments?post=21837"}],"version-history":[{"count":1,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/webstorm\/21837\/revisions"}],"predecessor-version":[{"id":213804,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/webstorm\/21837\/revisions\/213804"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/media?parent=21837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/categories?post=21837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/tags?post=21837"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/cross-post-tag?post=21837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}