<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NodeJS &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/nodejs/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 16:50:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>NodeJS &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Karma: Managing Plugin Versions in package.json</title>
		<link>https://myshittycode.com/2014/11/11/karma-managing-plugin-versions-in-package-json/</link>
					<comments>https://myshittycode.com/2014/11/11/karma-managing-plugin-versions-in-package-json/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 12 Nov 2014 02:20:38 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Karma]]></category>
		<category><![CDATA[NodeJS]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=670</guid>

					<description><![CDATA[<p>PROBLEM Let&#8217;s assume our package.json looks like this:- What we want to do is to update all the plugin versions defined in this file. SOLUTION After trying out several solutions, it appears that using npm-check-updates is a better and cleaner solution for discovering newer versions of these plugins. First, we need to install npm-check-updates globally. [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/11/11/karma-managing-plugin-versions-in-package-json/">Karma: Managing Plugin Versions in package.json</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Let&#8217;s assume our <code>package.json</code> looks like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
{
  &quot;name&quot;: &quot;testKarma&quot;,
  &quot;private&quot;: true,
  &quot;devDependencies&quot;: {
    &quot;karma&quot;: &quot;^0.12.24&quot;,
    &quot;karma-chrome-launcher&quot;: &quot;^0.1.4&quot;,
    &quot;karma-coverage&quot;: &quot;^0.2.4&quot;,
    &quot;karma-jasmine&quot;: &quot;^0.2.2&quot;,
    &quot;karma-junit-reporter&quot;: &quot;^0.2.1&quot;,
    &quot;karma-phantomjs-launcher&quot;: &quot;^0.1.4&quot;
  }
}
</pre></div>


<p>What we want to do is to update all the plugin versions defined in this file.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>After trying out several solutions, it appears that using <a href="https://www.npmjs.org/package/npm-check-updates" target="_blank" rel="noopener">npm-check-updates</a> is a better and cleaner solution for discovering newer versions of these plugins.</p>



<p>First, we need to install <i>npm-check-updates</i> globally. You may need to use <code>sudo</code> to do so.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
sudo npm install -g npm-check-updates
</pre></div>


<p>Once installed, run the following command within the project root directory to discover any new versions:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
npm-check-updates
</pre></div>


<p>Output:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
&quot;karma-chrome-launcher&quot; can be updated from ^0.1.4 to ^0.1.5 (Installed: 0.1.5, Latest: 0.1.5)
&quot;karma-coverage&quot; can be updated from ^0.2.4 to ^0.2.6 (Installed: 0.2.6, Latest: 0.2.6)
&quot;karma-jasmine&quot; can be updated from ^0.2.2 to ^0.2.3 (Installed: 0.2.3, Latest: 0.2.3)
&quot;karma-junit-reporter&quot; can be updated from ^0.2.1 to ^0.2.2 (Installed: 0.2.2, Latest: 0.2.2)

Run &#039;npm-check-updates -u&#039; to upgrade your package.json automatically
</pre></div>


<p>Finally, run the following command to update the plugins:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
npm-check-updates -u
</pre></div>


<p>Output:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
&quot;karma-chrome-launcher&quot; can be updated from ^0.1.4 to ^0.1.5 (Installed: 0.1.5, Latest: 0.1.5)
&quot;karma-coverage&quot; can be updated from ^0.2.4 to ^0.2.6 (Installed: 0.2.6, Latest: 0.2.6)
&quot;karma-jasmine&quot; can be updated from ^0.2.2 to ^0.2.3 (Installed: 0.2.3, Latest: 0.2.3)
&quot;karma-junit-reporter&quot; can be updated from ^0.2.1 to ^0.2.2 (Installed: 0.2.2, Latest: 0.2.2)

package.json upgraded
</pre></div>


<p>The plugin versions are successfully updated, and <code>package.json</code> is also updated accordingly.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
{
  &quot;name&quot;: &quot;testKarma&quot;,
  &quot;private&quot;: true,
  &quot;devDependencies&quot;: {
    &quot;karma&quot;: &quot;^0.12.24&quot;,
    &quot;karma-chrome-launcher&quot;: &quot;^0.1.5&quot;,
    &quot;karma-coverage&quot;: &quot;^0.2.6&quot;,
    &quot;karma-jasmine&quot;: &quot;^0.2.3&quot;,
    &quot;karma-junit-reporter&quot;: &quot;^0.2.2&quot;,
    &quot;karma-phantomjs-launcher&quot;: &quot;^0.1.4&quot;
  }
}
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2014/11/11/karma-managing-plugin-versions-in-package-json/">Karma: Managing Plugin Versions in package.json</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2014/11/11/karma-managing-plugin-versions-in-package-json/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">670</post-id>	</item>
		<item>
		<title>Jenkins: Getting Karma Generated Test Results to Appear in Maven Project Job</title>
		<link>https://myshittycode.com/2014/11/11/jenkins-getting-karma-generated-test-results-to-appear-in-maven-project-job/</link>
					<comments>https://myshittycode.com/2014/11/11/jenkins-getting-karma-generated-test-results-to-appear-in-maven-project-job/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Tue, 11 Nov 2014 14:00:42 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Jenkins]]></category>
		<category><![CDATA[Karma]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Maven Karma Plugin]]></category>
		<category><![CDATA[NodeJS]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=597</guid>

					<description><![CDATA[<p>PROBLEM Jenkins, for some reason, does not pick up Karma generated JUnit test reports even though they are created in the right directory&#8230; and apparently, it is a known problem. While Freestyle project job allows us to manually publish these JUnit reports, my intention is to rely on Maven project job to do the same [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/11/11/jenkins-getting-karma-generated-test-results-to-appear-in-maven-project-job/">Jenkins: Getting Karma Generated Test Results to Appear in Maven Project Job</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Jenkins, for some reason, does not pick up Karma generated JUnit test reports even though they are created in the right directory&#8230; and apparently, it is a known problem. While <b>Freestyle project</b> job allows us to manually publish these JUnit reports, my intention is to rely on <b>Maven project</b> job to do the same thing.</p>



<h2 class="wp-block-heading">PREREQUISITES</h2>



<ul class="wp-block-list">
<li><a href="http://myshittycode.com/2014/11/09/karma-getting-started/" target="_blank" rel="noopener">Karma: Getting Started</a></li>
</ul>



<h2 class="wp-block-heading">ENSURING KARMA GENERATED JUNIT REPORT SHOWS UP IN JENKINS</h2>



<p>Instead of manually running <strong>karma start</strong> command in Jenkins, we will rely on maven-karma-plugin to do this for us. The key here is to specify the correct <strong>&lt;phase&gt;</strong> so that Jenkins picks up and presents the generated report.</p>



<h3 class="wp-block-heading">pom.xml</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [9]; title: ; notranslate">
&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;com.kelveden&lt;/groupId&gt;
            &lt;artifactId&gt;maven-karma-plugin&lt;/artifactId&gt;
            &lt;version&gt;1.6&lt;/version&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                    &lt;phase&gt;process-test-classes&lt;/phase&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;start&lt;/goal&gt;
                    &lt;/goals&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
            &lt;configuration&gt;
                &lt;karmaExecutable&gt;${basedir}/node_modules/karma/bin/karma&lt;/karmaExecutable&gt;
                &lt;configFile&gt;src/test/resources/karma.jenkins.conf.js&lt;/configFile&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;
</pre></div>


<h2 class="wp-block-heading">CONFIGURING JENKINS</h2>



<p>Since Karma test runner requires NodeJS, we will install <a href="https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin" target="_blank" rel="noopener">NodeJS Plugin</a> in Jenkins. This allows us to automatically install NodeJS from Jenkins.</p>



<p>Once installed, go to <strong>Manage Jenkins -&gt; Configure System</strong> and go to NodeJS section:-</p>



<figure class="wp-block-image aligncenter"><img fetchpriority="high" decoding="async" width="1316" height="352" src="https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-45-09-pm-1.png?x45560" alt="" class="wp-image-609" srcset="https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-45-09-pm-1.png 1316w, https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-45-09-pm-1-300x80.png 300w, https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-45-09-pm-1-1024x274.png 1024w, https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-45-09-pm-1-768x205.png 768w" sizes="(max-width: 1316px) 100vw, 1316px" /></figure>



<p>Although this section allows us to specify npm packages to install, I&#8217;m having trouble installing certain packages, such as karma-phantomjs-launcher. The phantomJS package invokes <strong>node install.js</strong> during the installation, however, the <strong>node</strong> command isn&#8217;t available in <strong>PATH</strong> environment variable at this point. Thus, the installation will always fail. So, the npm packages will be configured at the job level in the next step. Further, <b>it is not a good idea to install Karma-related plugins globally</b> here because every Jenkins job may use slightly different versions of the same plugin (think Spring or Hibernate versions in every job&#8217;s <strong>pom.xml</strong>).</p>



<p>Next, create a <b>Maven project</b> job and configure it.</p>



<h3 class="wp-block-heading">Configuring Build Environment, Pre Steps and Build</h3>



<p>We exposed NodeJS to <strong>PATH</strong> environment variable so that we can install phantomJS package.</p>



<p>Next, we created a pre-build step to execute <strong>npm install</strong>, which reads <strong>package.json</strong> from the job and installs the needed dependencies within the job.</p>



<p>Finally, we will want Maven to invoke <strong>test</strong> goal so that it runs both Java tests and Karma test runner.</p>



<figure class="wp-block-image aligncenter"><img decoding="async" width="995" height="656" src="https://myshittycode.com/wp-content/uploads/2014/11/screen_shot_2014-11-11_at_8_28_24_am-1.png?x45560" alt="" class="wp-image-661" srcset="https://myshittycode.com/wp-content/uploads/2014/11/screen_shot_2014-11-11_at_8_28_24_am-1.png 995w, https://myshittycode.com/wp-content/uploads/2014/11/screen_shot_2014-11-11_at_8_28_24_am-1-300x198.png 300w, https://myshittycode.com/wp-content/uploads/2014/11/screen_shot_2014-11-11_at_8_28_24_am-1-768x506.png 768w" sizes="(max-width: 995px) 100vw, 995px" /></figure>



<h3 class="wp-block-heading">Configuring Coverage Report</h3>



<p>We provided the Karma generated coverage report XML file.</p>



<figure class="wp-block-image aligncenter"><img decoding="async" width="973" height="210" src="https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-05-51-pm-1.png?x45560" alt="" class="wp-image-611" srcset="https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-05-51-pm-1.png 973w, https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-05-51-pm-1-300x65.png 300w, https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-05-51-pm-1-768x166.png 768w" sizes="(max-width: 973px) 100vw, 973px" /></figure>



<h2 class="wp-block-heading">OUTCOME</h2>



<p>When we run <b>Build Now</b> in Jenkins, both unit test and coverage reports will display both Java and JavaScript execution results.</p>



<figure class="wp-block-image aligncenter"><img loading="lazy" decoding="async" width="781" height="266" src="https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-26-36-pm-1.png?x45560" alt="" class="wp-image-612" srcset="https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-26-36-pm-1.png 781w, https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-26-36-pm-1-300x102.png 300w, https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-26-36-pm-1-768x262.png 768w" sizes="auto, (max-width: 781px) 100vw, 781px" /></figure>



<figure class="wp-block-image aligncenter"><img loading="lazy" decoding="async" width="781" height="733" src="https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-26-12-pm-1.png?x45560" alt="" class="wp-image-613" srcset="https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-26-12-pm-1.png 781w, https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-26-12-pm-1-300x282.png 300w, https://myshittycode.com/wp-content/uploads/2014/10/screen-shot-2014-10-23-at-1-26-12-pm-1-768x721.png 768w" sizes="auto, (max-width: 781px) 100vw, 781px" /></figure>



<p>Since we ran <strong>npm install</strong> as a pre-build step, the job will now have <strong>node_modules</strong> directory.</p>



<figure class="wp-block-image aligncenter"><img loading="lazy" decoding="async" width="538" height="254" src="https://myshittycode.com/wp-content/uploads/2014/11/screen_shot_2014-11-11_at_8_34_34_am-1.png?x45560" alt="" class="wp-image-662" srcset="https://myshittycode.com/wp-content/uploads/2014/11/screen_shot_2014-11-11_at_8_34_34_am-1.png 538w, https://myshittycode.com/wp-content/uploads/2014/11/screen_shot_2014-11-11_at_8_34_34_am-1-300x142.png 300w" sizes="auto, (max-width: 538px) 100vw, 538px" /></figure>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/11/11/jenkins-getting-karma-generated-test-results-to-appear-in-maven-project-job/">Jenkins: Getting Karma Generated Test Results to Appear in Maven Project Job</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2014/11/11/jenkins-getting-karma-generated-test-results-to-appear-in-maven-project-job/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">597</post-id>	</item>
		<item>
		<title>Karma: Getting Started</title>
		<link>https://myshittycode.com/2014/11/09/karma-getting-started/</link>
					<comments>https://myshittycode.com/2014/11/09/karma-getting-started/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Sun, 09 Nov 2014 19:09:58 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Karma]]></category>
		<category><![CDATA[NodeJS]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=627</guid>

					<description><![CDATA[<p>Overview This tutorial walks through the steps need to configure Karma to work with a Maven web project. It will also be used as a base for my future Karma related posts. Install Node.js First, download Node.js from http://nodejs.org/download/ and install it. Once installed, we should be able to invoke npm command from the command [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/11/09/karma-getting-started/">Karma: Getting Started</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Overview</h2>



<p>This tutorial walks through the steps need to configure Karma to work with a Maven web project. It will also be used as a base for my future Karma related posts.</p>



<h2 class="wp-block-heading">Install Node.js</h2>



<p>First, download Node.js from http://nodejs.org/download/ and install it. Once installed, we should be able to invoke <b>npm</b> command from the command line, for example:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
npm -version
</pre></div>


<h2 class="wp-block-heading">Create package.json</h2>



<p>Let&#8217;s assume we have the following Maven project:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
testKarma
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   ├── resources
│   │   └── webapp
│   └── test
│       └── java
└── testKarma.iml
</pre></div>


<p>Please ignore <b>testKarma.iml</b>, which is an IntelliJ specific file.</p>



<p>Create a file called <b>package.json</b> under project root directory&#8230;</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [2]; title: ; notranslate">
testKarma
├── package.json
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   ├── resources
│   │   └── webapp
│   └── test
│       └── java
└── testKarma.iml
</pre></div>


<p>&#8230; with the following content&#8230;</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
{
  &quot;name&quot;: &quot;testKarma&quot;,
  &quot;private&quot;: true
}
</pre></div>


<p>In this case, we created a private repository by setting <b>private</b> to <b>true</b>.</p>



<p>Without this flag set, when you run <b>npm install &#8230;</b>, we will get these annoying warnings:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
npm WARN package.json testKarma@ No description
npm WARN package.json testKarma@ No repository field.
npm WARN package.json testKarma@ No README data
</pre></div>


<h2 class="wp-block-heading">Install Karma and Plugins</h2>



<p>From the project root directory, run the following command:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
npm install karma karma-jasmine@0.2.2 karma-chrome-launcher karma-phantomjs-launcher karma-junit-reporter karma-coverage --save-dev
</pre></div>


<p>At the time of this post, by default, <b>karma-jasmine</b> will install version 0.1.5. So, we will manually specify the latest version that allows us to use Jasmine 2.x.</p>



<p>For local testing, we will run our tests against both Chrome browser and PhantomJS, which is a headless browser. So, make sure Chrome browser is already installed.</p>



<p>The project structure now contains the installed plugins for JavaScript testing:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [2,3,4,5,6,7,8]; title: ; notranslate">
testKarma
├── node_modules
│   ├── karma
│   ├── karma-chrome-launcher
│   ├── karma-coverage
│   ├── karma-jasmine
│   ├── karma-junit-reporter
│   └── karma-phantomjs-launcher
├── package.json
├── pom.xml
├── src
│   ├── main
│   └── test
└── testKarma.iml
</pre></div>


<p>The <b>package.json</b> contains a history of the plugins we installed within this project:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; highlight: [4,5,6,7,8,9,10,11]; title: ; notranslate">
{
  &quot;name&quot;: &quot;testKarma&quot;,
  &quot;private&quot;: true,
  &quot;devDependencies&quot;: {
    &quot;karma&quot;: &quot;^0.12.24&quot;,
    &quot;karma-chrome-launcher&quot;: &quot;^0.1.5&quot;,
    &quot;karma-coverage&quot;: &quot;^0.2.6&quot;,
    &quot;karma-jasmine&quot;: &quot;^0.2.2&quot;,
    &quot;karma-junit-reporter&quot;: &quot;^0.2.2&quot;,
    &quot;karma-phantomjs-launcher&quot;: &quot;^0.1.4&quot;
  }
}
</pre></div>


<p>A couple of important notes:-</p>



<ul class="wp-block-list">
<li>Basically, since we are not running <b>npm install</b> with <b>-g</b> option, the plugins will not be installed globally. Rather, they are installed within the project root directory.</li>



<li>Using <b>&#8211;save-dev</b> option, if <b>package.json</b> exists, this file will be automatically updated to keep track of the installed plugins and versions.</li>



<li>If we decided to update the plugin versions, we just need to modify this file and run <b>npm install</b> to update them.</li>



<li>We do not want to commit <b>node_modules</b> directory into any VCS because there are at least 5000 files in this directory. So, <b>remember to configure a VCS exclusion on this directory</b> (see <a href="http://myshittycode.com/2014/08/05/intellij-handling-svn-global-ignore-list/" target="_blank" rel="noopener">IntelliJ: Handling SVN Global Ignore List</a>).</li>



<li>When other peers check out this project from VCS, they will run <b>npm install</b>, which will automatically install all the dependencies listed in <b>package.json</b>.</li>
</ul>



<h2 class="wp-block-heading">Create Karma Configuration File</h2>



<p>Instead of running <b>karma init karma.conf.js</b> to step through the process to create a configuration file, we will manually create two Karma configuration files under <b>src/test/resources</b> directory.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [12,13,14]; title: ; notranslate">
testKarma
├── node_modules
├── package.json
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   ├── resources
│   │   └── webapp
│   └── test
│       ├── java
│       └── resources
│           ├── karma.conf.js
│           └── karma.jenkins.conf.js
</pre></div>


<h3 class="wp-block-heading">karma.conf.js</h3>



<p>This configuration is used for local testing.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
module.exports = function ( config ) {
    config.set( {
        basePath         : &#039;../../../&#039;,
        frameworks       : &#x5B;&#039;jasmine&#039;],
        files            : &#x5B;
            &#039;src/main/webapp/resources/js/**/*.js&#039;,
            &#039;src/test/js/**/*.js&#039;
        ],
        exclude          : &#x5B;],
        preprocessors    : {
            &#039;src/main/webapp/resources/js/**/*.js&#039; : &#x5B;&#039;coverage&#039;]
        },
        reporters        : &#x5B;&#039;progress&#039;, &#039;coverage&#039;],
        port             : 9876,
        colors           : true,
        logLevel         : config.LOG_INFO,
        autoWatch        : true,
        browsers         : &#x5B;&#039;Chrome&#039;, &#039;PhantomJS&#039;],
        singleRun        : false,
        plugins          : &#x5B;
            &#039;karma-jasmine&#039;,
            &#039;karma-chrome-launcher&#039;,
            &#039;karma-phantomjs-launcher&#039;,
            &#039;karma-junit-reporter&#039;,
            &#039;karma-coverage&#039;
        ],
        coverageReporter : {
            type : &#039;html&#039;,
            dir  : &#039;target/coverage/&#039;
        }
    } );
};
</pre></div>


<h3 class="wp-block-heading">karma.jenkins.conf.js</h3>



<p>This configuration is used for automated testing on Jenkins.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
module.exports = function ( config ) {
    config.set( {
        basePath         : &#039;../../../&#039;,
        frameworks       : &#x5B;&#039;jasmine&#039;],
        files            : &#x5B;
            &#039;src/main/webapp/resources/js/**/*.js&#039;,
            &#039;src/test/js/**/*.js&#039;
        ],
        exclude          : &#x5B;],
        preprocessors    : {
            &#039;src/main/webapp/resources/js/**/*.js&#039; : &#x5B;&#039;coverage&#039;]
        },
        // added `junit`
        reporters        : &#x5B;&#039;progress&#039;, &#039;junit&#039;, &#039;coverage&#039;],
        port             : 9876,
        colors           : true,
        logLevel         : config.LOG_INFO,
        // don&#039;t watch for file change
        autoWatch        : false,
        // only runs on headless browser
        browsers         : &#x5B;&#039;PhantomJS&#039;],
        // just run one time
        singleRun        : true,
        // remove `karma-chrome-launcher` because we will be running on headless
        // browser on Jenkins
        plugins          : &#x5B;
            &#039;karma-jasmine&#039;,
            &#039;karma-phantomjs-launcher&#039;,
            &#039;karma-junit-reporter&#039;,
            &#039;karma-coverage&#039;
        ],
        // changes type to `cobertura`
        coverageReporter : {
            type : &#039;cobertura&#039;,
            dir  : &#039;target/coverage-reports/&#039;
        },
        // saves report at `target/surefire-reports/TEST-*.xml` because Jenkins
        // looks for this location and file prefix by default.
        junitReporter    : {
            outputFile : &#039;target/surefire-reports/TEST-karma-results.xml&#039;
        }
    } );
};
</pre></div>


<h2 class="wp-block-heading">Write JS Production Code and Test Code</h2>



<p>Now, it&#8217;s time to write some tests and run them.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [10,11,12,15,16]; title: ; notranslate">
testKarma
├── node_modules
├── package.json
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   ├── resources
│   │   └── webapp
│   │       └── resources
│   │           └── js
│   │               └── hello.js
│   └── test
│       ├── java
│       ├── js
│       │   └── hello-spec.js
│       └── resources
│           ├── karma.conf.js
│           └── karma.jenkins.conf.js
└── testKarma.iml
</pre></div>


<h3 class="wp-block-heading">hello.js</h3>



<p>We will keep our production code to the minimum in this example:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
var hello = {
    speak : function () {
        return &#039;Hello!&#039;;
    }
};
</pre></div>


<h3 class="wp-block-heading">hello-spec.js</h3>



<p>A very simple test case:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
describe( &#039;hello module&#039;, function () {
	&#039;use strict&#039;;
    it( &#039;speak()&#039;, function () {
        expect( hello.speak() ).toBe( &#039;Hello!&#039; );
    } );
} );
</pre></div>


<h2 class="wp-block-heading">Run Karma using karma.conf.js</h2>



<p>From the project root directory, run Karma:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
node_modules/karma/bin/karma start src/test/resources/karma.conf.js
</pre></div>


<p>The console should now look like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
INFO &#x5B;karma]: Karma v0.12.24 server started at http://localhost:9876/
INFO &#x5B;launcher]: Starting browser Chrome
INFO &#x5B;launcher]: Starting browser PhantomJS
INFO &#x5B;PhantomJS 1.9.8 (Mac OS X)]: Connected on socket YNMziWTsyeaf6DZzw06D with id 3320523
INFO &#x5B;Chrome 38.0.2125 (Mac OS X 10.9.5)]: Connected on socket rYQeni1xm1bbqfa3w06E with id 50259203
PhantomJS 1.9.8 (Mac OS X): Executed 1 of 1 SUCCESS (0.003 secs / 0.001 secs)
Chrome 38.0.2125 (Mac OS X 10.9.5): Executed 1 of 1 SUCCESS (0.008 secs / 0.001 secs)
TOTAL: 2 SUCCESS
</pre></div>


<p>The <b>target/coverage</b> directory should contain Chrome and PhantomJS subdirectories.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]; title: ; notranslate">
testKarma
├── node_modules
├── package.json
├── pom.xml
├── src
├── target
│   └── coverage
│       ├── Chrome 38.0.2125 (Mac OS X 10.9.5)
│       │   ├── index.html
│       │   ├── js
│       │   │   ├── hello.js.html
│       │   │   └── index.html
│       │   ├── prettify.css
│       │   └── prettify.js
│       └── PhantomJS 1.9.8 (Mac OS X)
│           ├── index.html
│           ├── js
│           │   ├── hello.js.html
│           │   └── index.html
│           ├── prettify.css
│           └── prettify.js
└── testKarma.iml
</pre></div>


<p>When opening one of the <b>index.html</b> files, we should see the coverage report.</p>



<figure class="wp-block-image aligncenter"><img loading="lazy" decoding="async" width="1570" height="380" src="https://myshittycode.com/wp-content/uploads/2014/11/screen-shot-2014-11-09-at-12-18-08-pm-1.png?x45560" alt="" class="wp-image-639" srcset="https://myshittycode.com/wp-content/uploads/2014/11/screen-shot-2014-11-09-at-12-18-08-pm-1.png 1570w, https://myshittycode.com/wp-content/uploads/2014/11/screen-shot-2014-11-09-at-12-18-08-pm-1-300x73.png 300w, https://myshittycode.com/wp-content/uploads/2014/11/screen-shot-2014-11-09-at-12-18-08-pm-1-1024x248.png 1024w, https://myshittycode.com/wp-content/uploads/2014/11/screen-shot-2014-11-09-at-12-18-08-pm-1-768x186.png 768w, https://myshittycode.com/wp-content/uploads/2014/11/screen-shot-2014-11-09-at-12-18-08-pm-1-1536x372.png 1536w" sizes="auto, (max-width: 1570px) 100vw, 1570px" /></figure>



<h2 class="wp-block-heading">Run Karma using karma.jenkins.conf.js</h2>



<p>Although we will only use <b>karma.jenkins.conf.js</b> for automated testing on Jenkins, we will run this configuration file to see the generated output differences.</p>



<p>Clean the <b>target</b> directory:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
mvn clean
</pre></div>


<p>Run Karma with <b>karma.jenkins.conf.js</b> instead:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
node_modules/karma/bin/karma start src/test/resources/karma.jenkins.conf.js
</pre></div>


<p>Since this configuration only runs on the headless browser, we will not see Chrome results here:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
INFO &#x5B;karma]: Karma v0.12.24 server started at http://localhost:9876/
INFO &#x5B;launcher]: Starting browser PhantomJS
INFO &#x5B;PhantomJS 1.9.8 (Mac OS X)]: Connected on socket SM_5sy2wzHdL4ru9yg0X with id 42922522
PhantomJS 1.9.8 (Mac OS X): Executed 1 of 1 SUCCESS (0.002 secs / 0.001 secs)
</pre></div>


<p>The <b>target</b> directory should contain <b>cobertura-coverage.xml</b> for coverage result and <b>TEST-karma-results.xml</b> for test result.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [6,7,8,9,10,11]; title: ; notranslate">
testKarma
├── node_modules
├── package.json
├── pom.xml
├── src
├── target
│   ├── coverage-reports
│   │   └── PhantomJS 1.9.8 (Mac OS X)
│   │       └── cobertura-coverage.xml
│   └── surefire-reports
│       └── TEST-karma-results.xml
└── testKarma.iml
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2014/11/09/karma-getting-started/">Karma: Getting Started</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2014/11/09/karma-getting-started/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">627</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-02-21 01:47:08 by W3 Total Cache
-->