<?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>jest Archives - Yumasoft</title>
	<atom:link href="https://blog.yumasoft.pl/tag/jest/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.yumasoft.pl/tag/jest/</link>
	<description>Software development blog</description>
	<lastBuildDate>Wed, 14 Jul 2021 04:17:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.yumasoft.pl/wp-content/uploads/2021/05/cropped-yumasoft_icon_transparent-32x32.png</url>
	<title>jest Archives - Yumasoft</title>
	<link>https://blog.yumasoft.pl/tag/jest/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How To Use StrykerJS With jest And TypeScript</title>
		<link>https://blog.yumasoft.pl/2021/07/how-to-use-strykerjs-with-jest-and-typescript/</link>
					<comments>https://blog.yumasoft.pl/2021/07/how-to-use-strykerjs-with-jest-and-typescript/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Wed, 14 Jul 2021 06:00:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jest]]></category>
		<category><![CDATA[mutation tests]]></category>
		<category><![CDATA[software testing]]></category>
		<category><![CDATA[stryker]]></category>
		<category><![CDATA[stryker mutator]]></category>
		<category><![CDATA[strykerjs]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=407</guid>

					<description><![CDATA[<p>Continuing the topic of frontend tests with jest, we&#8217;re taking a look at StrykerJS today. Stryker Mutator is one of the most popular tools for mutation testing. We recently struggled with configuring StykerJS to work properly with jest and TypeScript. I&#8217;d like to share our solution to these problems in this article. Jest config First,&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/07/how-to-use-strykerjs-with-jest-and-typescript/">How To Use StrykerJS With jest And TypeScript</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Continuing <a href="https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/">the topic of frontend tests</a> with <code>jest</code>, we&#8217;re taking a look at StrykerJS today. Stryker Mutator is one of the most popular tools for mutation testing. We recently struggled with configuring StykerJS to work properly with <code>jest</code> and TypeScript. I&#8217;d like to share our solution to these problems in this article.</p>



<span id="more-407"></span>



<h2 class="wp-block-heading">Jest config</h2>



<p>First, make sure that your <a href="https://jestjs.io/docs/configuration"><code>jest</code> configuration</a> is correct. If you&#8217;re with TypeScript (and you <a href="https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/">should</a> ?), I recommend using <a href="https://www.npmjs.com/package/ts-jest">ts-jest</a> configuration preset. Our config looks as follows:</p>



<script src="https://gist.github.com/dsibinski/04419106e45b2136f63c2255a8a77fba.js"></script>



<p>If everything is fine, you should be able to execute <code>npm test</code> command (assuming it&#8217;s defined in <code>package.json</code> as <code>"test": "jest"</code>) without issues. All your tests should be executed.</p>



<p>We now need to make StrykerJS working with that.</p>



<h2 class="wp-block-heading">StrykerJS with jest configuration</h2>



<p>If you install <a href="https://www.npmjs.com/package/@stryker-mutator/core">StrykerJS</a> as described <a href="https://github.com/stryker-mutator/stryker-js">here</a>, you&#8217;d expect that <code>npx stryker run</code> command runs mutations and everything is just fine. However, if you&#8217;re having wierd issues and errors with that, it probably means that Stryker is not properly configured to execute tests with <code>jest</code>.</p>



<p>When you run <code>npx stryker run</code>, it creates mutants (modifications in your source code) and runs tests against them. It means that StrykerJS must run your tests in the same way you run them. So, if you run tests with <code>jest</code>, Stryker must be able to do the same.</p>



<p>We struggled with many issues related to running StrykerJS with <code>jest</code>. Some of them were related purely to <code>jest</code>, but some of them to TypeScript as well. After numerous trial-and-errors and consulting with StrykerJS community members, we worked out a configuration that finally works fine for us.</p>



<p>Going straight to the point, here&#8217;s the <code>stryker.conf.json</code> file that would make StrykerJS work fine with <code>jest</code> and TypeScript:</p>



<script src="https://gist.github.com/dsibinski/116b47fe141aaf7f71b2e8f1c83137b2.js"></script>



<p>To make it work, install the following npm packages: <a href="https://www.npmjs.com/package/@stryker-mutator/core">@stryker-mutator/core</a>, <a href="https://www.npmjs.com/package/@stryker-mutator/jest-runner">@stryker-mutator/jest-runner</a> and <a href="https://www.npmjs.com/package/@stryker-mutator/typescript-checker">@stryker-mutator/typescript-checker</a>.</p>



<p>Notice few important settings here:</p>



<ul class="wp-block-list"><li> <code>"checkers": ["typescript"]</code> &#8211; this enabled a <a href="https://stryker-mutator.io/docs/stryker-js/typescript-checker/">TypeScript checker</a> plugin, which makes mutations being checked for types before running tests on them. It means that if a mutation introduced by Stryker doesn&#8217;t compile because of types issues, it won&#8217;t be taken into account for mutations (which makes perfect sense)</li><li>&#8220;<code>disableTypeChecks</code>&#8221; &#8211; you still need to tell StrykerJS to not check types when compiling your code with type errors (mutants). It will compile them without types checking, but thanks to TypeScript checker plugin these ones will be filtered out for mutations</li><li><code>"tsconfigFile": "tsconfig.json"</code> &#8211; make sure to point to the TS config file you&#8217;re using here</li><li><code>"concurrency": 4</code>: this is another setting we experimented with to make Stryker working correctly. Without it, we were getting weird errors about lack of memory when running mutations (<code>FATAL ERROR: Committing semi space failed. Allocation failed - JavaScript heap out of memory</code>)</li><li><code>"testRunner": "jest"</code> &#8211; this one enables the <code>jest</code> as our tests runner</li><li><code>"jest"</code> &#8211; here you tell Stryker to use your <code>jest.config.js</code> file we talked about at the beginning</li></ul>



<p>With these settings, I hope that you are able to see this beautiful output of <code>npx stryker run</code> in your console:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="827" height="156" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/Stryker_mutations_result.png" alt="" class="wp-image-414" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/Stryker_mutations_result.png 827w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/Stryker_mutations_result-300x57.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/Stryker_mutations_result-768x145.png 768w" sizes="(max-width: 827px) 100vw, 827px" /></figure></div>



<h2 class="wp-block-heading">StrykerJS with jest and TypeScript &#8211; summary</h2>



<p>I hope this is helpful for you. StrykerJS is a developing framework, so some things are still hard to figure out. We believe mutation tests are a great way of improving our tests, so we use them on a daily basis. If you ever have any issues with StrykerJS, I encourage you to reach out to <a href="https://join.slack.com/t/stryker-mutator/shared_invite/enQtOTUyMTYyNTg1NDQ0LTU4ODNmZDlmN2I3MmEyMTVhYjZlYmJkOThlNTY3NTM1M2QxYmM5YTM3ODQxYmJjY2YyYzllM2RkMmM1NjNjZjM">the community on Slack</a>. They are extremely helpful there ?</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/07/how-to-use-strykerjs-with-jest-and-typescript/">How To Use StrykerJS With jest And TypeScript</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/07/how-to-use-strykerjs-with-jest-and-typescript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How To Configure Jest Tests on TeamCity</title>
		<link>https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/</link>
					<comments>https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 12 Jul 2021 10:30:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jest]]></category>
		<category><![CDATA[jest teamcity]]></category>
		<category><![CDATA[jest teamcity reporter]]></category>
		<category><![CDATA[teamcity]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=381</guid>

					<description><![CDATA[<p>If you are using jest to write JavaScript/TypeScript tests like this one: and you&#8217;re looking for how to show their results in TeamCity as follows: Then this article will help you configure that ? Prerequisites To run jest tests on TeamCity, you need to use jest as your testing framework of course ? If you&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/">How To Configure Jest Tests on TeamCity</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>If you are using <code>jest</code> to write JavaScript/TypeScript tests like this one:</p>



<script src="https://gist.github.com/dsibinski/5266776ab31d225e6dc97278d4f5f02f.js"></script>



<p>and you&#8217;re looking for how to show their results in TeamCity as follows:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="696" height="314" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_jest_tests_result-1.png" alt="Jest tests on TeamCity - sample output" class="wp-image-387" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_jest_tests_result-1.png 696w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_jest_tests_result-1-300x135.png 300w" sizes="(max-width: 696px) 100vw, 696px" /></figure></div>



<p>Then this article will help you configure that ?</p>



<span id="more-381"></span>



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



<p>To run <code>jest</code> tests on TeamCity, you need to use <a href="https://www.npmjs.com/package/jest">jest</a> as your testing framework of course ? If you use TypeScript &#8211; which I <a href="https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/">definitely recommend</a> &#8211; <a href="https://www.npmjs.com/package/ts-jest">ts-jest</a> package will also be useful.</p>



<p>Having these packages installed, your basic <code>jest.config.js</code> file should look similar to this:</p>



<script src="https://gist.github.com/dsibinski/adb20170c230f922b2ebf0f0b90b7975.js"></script>



<p>You should also have a <code>test</code> script defined in <code>scripts</code> section of your <code>package.json</code> file:</p>



<script src="https://gist.github.com/dsibinski/751a5df5169684cc44eb3d4497e3bb2a.js"></script>



<p>With this setup, you are able to execute your tests with <code>npm test</code> command and see the results in the console:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="781" height="272" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/jest_tests_results_console.png" alt="Jest tests output in VS Code" class="wp-image-391" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/jest_tests_results_console.png 781w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/jest_tests_results_console-300x104.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/jest_tests_results_console-768x267.png 768w" sizes="(max-width: 781px) 100vw, 781px" /></figure></div>



<p>Such tests results are however not recognized in any way by <a href="https://www.jetbrains.com/teamcity/">TeamCity</a> build server. Let&#8217;s see how to configure that.</p>



<h2 class="wp-block-heading">Installing TeamCity reporter</h2>



<p>In order to see the tests results in TeamCity, we need the <code>npm test</code> command to produce a different output when run on TC server. This output must be compatible with <a href="https://www.jetbrains.com/help/teamcity/service-messages.html#Reporting+Tests">TeamCity&#8217;s service messages</a>.</p>



<p>Fortunately, you don&#8217;t need to implement it by yourself ?</p>



<p>First, install <a href="https://www.npmjs.com/package/jest-teamcity">jest-teamcity</a> npm package to your project. Next, add <code>jest-teamcity</code> to <code>reporters</code> configuration option in your <code>jest.config.js</code> file:</p>



<script src="https://gist.github.com/dsibinski/7e9a94a8374d5901d452844e8a4fc9d5.js"></script>



<h2 class="wp-block-heading">Adding TeamCity build step</h2>



<p>Next, login to your TeamCity server. You need to know the location of <code>npm.cmd</code> file on your TeamCity server. It should be in NodeJS installation catalog. In our case, the path to the file is <code>C:\Dev\nodejs\npm.cmd</code>. </p>



<p>Now, go to TeamCity web administration interface and create a new build configuration or edit a current one. Then, add a new build step:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="618" height="212" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_add_new_step.png" alt="TeamCity - adding a new build step button" class="wp-image-393" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_add_new_step.png 618w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_add_new_step-300x103.png 300w" sizes="auto, (max-width: 618px) 100vw, 618px" /></figure></div>



<p>Configure the new step as follows:</p>



<ul class="wp-block-list"><li><strong>Runner type</strong>: <em>Command line</em></li><li><strong>Step name</strong>: <em>Jest JS tests</em></li><li><strong>Execute step</strong>: <em>If all previous steps finished successfully</em> (or anything else you prefer)</li><li><strong>Working directory</strong>: name of the catalog in your web app where you normally run <code>npm test</code></li><li><strong>Run</strong>: <em>Custom script</em></li><li><strong>Custom script</strong>: <em>&#8220;your_path_to_nodejs\npm.cmd&#8221; run test</em>, for example: <em>&#8220;C:\Dev\nodejs\npm.cmd&#8221; run test</em></li><li><strong>Format stderr output as</strong>: <em>error</em></li></ul>



<p>If everything is configured correctly, during the next build you should see <code>jest</code> tests output recognized and nicely reported by TeamCity:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="696" height="314" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_jest_tests_result-2.png" alt="Jest tests on TeamCity - jest tests results in a build" class="wp-image-394" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_jest_tests_result-2.png 696w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_jest_tests_result-2-300x135.png 300w" sizes="auto, (max-width: 696px) 100vw, 696px" /></figure></div>



<p>If that&#8217;s not the case, make sure that <code>TEAMCITY_VERSION</code> environment variable is set on your TeamCity machine. That&#8217;s how <code>jest-teamcity</code> reporter recognizes when to output standard <code>jest</code> output and when to use TeamCity&#8217;s service messages format.</p>



<h2 class="wp-block-heading">Debugging jest tests on TeamCity locally</h2>



<p>If you run <code>npm test</code> locally, you will see that the output is still normal, not the TeamCity format. That&#8217;s correct, because as we said before, the <code>TEAMCITY_VERSION</code> environment variable must exist in order to see TeamCity-formatted output. Normally we don&#8217;t want to see TeamCity-formatted tests output locally, of course ?</p>



<p>It means that if you want to debug this configuration locally and actually see the TC tests output on your dev machine, you need to define such environment variable locally.</p>



<p>In order to do that in Windows, go to <code>Edit the system environment variables</code>, click on <code>Evironment Variables...</code> and add a new one in <code>System variables</code> section. Call this variable <code>TEAMCITY_VERSION</code> and give it some value, for example <code>2021.1</code>:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="653" height="168" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_version_evironment_variable.png" alt="Adding new system environment variable called &quot;TEAMCITY_VERSION&quot; with value of &quot;2021.1&quot;, so the jest reporter can output TeamCity-formatted result" class="wp-image-396" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_version_evironment_variable.png 653w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_version_evironment_variable-300x77.png 300w" sizes="auto, (max-width: 653px) 100vw, 653px" /></figure></div>



<p>If you&#8217;re on another operating system, add the environment variable according to your system&#8217;s requirements.</p>



<p>Next, restart Visual Studio Code or the console tool you are using and execute <code>npm test</code> again. Now you should see the tests results output in TeamCity format:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="214" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/npm_test_jest_teamcity_format-1024x214.png" alt="TeamCity-formatted jest tests results in VS Code console" class="wp-image-397" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/npm_test_jest_teamcity_format-1024x214.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/npm_test_jest_teamcity_format-300x63.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/npm_test_jest_teamcity_format-768x161.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/npm_test_jest_teamcity_format.png 1127w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<h2 class="wp-block-heading">Jest tests in TeamCity &#8211; summary</h2>



<p>I hope you found this piece of advice useful. At Yumasoft, we value tests a lot, both backend and frontend ones. However, the tests which are not run automatically are worth nothing. That&#8217;s why we always have all of our tests run automatically as part of our CI process (TeamCity in the sample project&#8217;s case).</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/">How To Configure Jest Tests on TeamCity</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
