<?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>javascript Archives - Yumasoft</title>
	<atom:link href="https://blog.yumasoft.pl/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.yumasoft.pl/tag/javascript/</link>
	<description>Software development blog</description>
	<lastBuildDate>Wed, 24 Nov 2021 02:06:15 +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>javascript Archives - Yumasoft</title>
	<link>https://blog.yumasoft.pl/tag/javascript/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Webpack Watch Cached And Not Refreshing Bundles</title>
		<link>https://blog.yumasoft.pl/2021/11/webpack-watch-cached-and-not-refreshing-bundles/</link>
					<comments>https://blog.yumasoft.pl/2021/11/webpack-watch-cached-and-not-refreshing-bundles/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Wed, 24 Nov 2021 06:00:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[webpack]]></category>
		<category><![CDATA[webpack watch]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=829</guid>

					<description><![CDATA[<p>If you use webpack as your JavaScript bundler, you might have experienced an issue with webpack --watch not refreshing your bundles when modifying the source files. In this case, webpack&#8217;s output after changing the file is similar to the following one: As you can see, there are some [cached] mentions there in a few places&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/11/webpack-watch-cached-and-not-refreshing-bundles/">Webpack Watch Cached And Not Refreshing Bundles</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>If you use <a href="https://webpack.js.org/">webpack</a> as your JavaScript bundler, you might have experienced an issue with <code>webpack --watch</code> not refreshing your bundles when modifying the source files. In this case, webpack&#8217;s output after changing the file is similar to the following one:</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="87" src="https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchNotRefreshingCache-1024x87.png" alt="Webpack output showing [cached]" class="wp-image-830" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchNotRefreshingCache-1024x87.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchNotRefreshingCache-300x25.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchNotRefreshingCache-768x65.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchNotRefreshingCache.png 1112w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>As you can see, there are some <code>[cached]</code> mentions there in a few places and no <code>[code generated]</code> statements at all. The effect is that your bundles are not rebuilt at all. In order to have them refreshed, you need to restart the building process, which takes time and makes your development flow painful. If you want to know how to solve that problem, read on ?</p>



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



<h2 class="wp-block-heading">webpack watch cached files</h2>



<p>We have struggled with this issue for quite some time. It starts with launching <code>webpack</code> with <code>--watch</code> option. Such build script can be defined in your <code>package.json</code> in the following or similar way:</p>



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



<p>The effect you would expect is for webpack to rebuild your bundles as soon as you make any change in the source code.</p>



<p>When the webpack watch cached issue occurs, the first change you make in a file is properly reloaded by <code>webpack</code> and looks similarly to that:</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="375" src="https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchCorrectRefreshFirstTime-1024x375.png" alt="webpack watch correctly rebuilding bundles with [code generated] statements" class="wp-image-832" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchCorrectRefreshFirstTime-1024x375.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchCorrectRefreshFirstTime-300x110.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchCorrectRefreshFirstTime-768x281.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchCorrectRefreshFirstTime.png 1048w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>However, as soon as you change the file for the second time, the output looks less promising:</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="87" src="https://blog.yumasoft.pl/wp-content/uploads/2021/11/1_WebpackWatchNotRefreshingCache-1024x87.png" alt="webpack watch cached issue" class="wp-image-833" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/11/1_WebpackWatchNotRefreshingCache-1024x87.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/1_WebpackWatchNotRefreshingCache-300x25.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/1_WebpackWatchNotRefreshingCache-768x65.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/1_WebpackWatchNotRefreshingCache.png 1112w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>As soon as you see some of these <code>[cached]</code> things, it probably means that webpack cached something and did not generate new bundles. Especially if there are no <code>[built]</code> and <code>[code generated]</code> statements in the output. You notice that your files are not refreshed, so your last change is not reflected in JS bundles ?</p>



<h2 class="wp-block-heading">Hotfix: disable webpack cache</h2>



<p>The first hotfix solution &#8211; especially if you can&#8217;t easily find the cause &#8211; is to disable <code>webpack</code> cache completely. In order to do that, you should set <code>cache</code> to <code>false</code> in your <a href="https://webpack.js.org/configuration/cache/">webpack configuration file</a>:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="413" height="216" src="https://blog.yumasoft.pl/wp-content/uploads/2021/11/3_WebpackDisableCache.png" alt="cache set to false in webpack config file" class="wp-image-836" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/11/3_WebpackDisableCache.png 413w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/3_WebpackDisableCache-300x157.png 300w" sizes="auto, (max-width: 413px) 100vw, 413px" /></figure></div>



<p>This will work, and webpack should now rebuild your bundles on every source change. However, it comes with a significant performance cost. Disabling the cache will make webpack actually rebuilding all your bundles on each change. This solution is actually a shortcut for restarting the build from scratch. Not what we really want ? Let&#8217;s see how to find the cause and solve webpack watch cached issue properly.</p>



<h2 class="wp-block-heading">Wrong casing &#8211; the real cause of webpack watch cached issue</h2>



<p>In our case, the problem turned out to basically be <strong>wrong casing when importing stuff</strong>. </p>



<p>The general advice is to find how given file you are modifying is entered into your webpack&#8217;s build flow. It can be <a href="https://flexiple.com/javascript-require-vs-import/">imported or required</a> directly. Maybe it&#8217;s used via <a href="https://webpack.js.org/configuration/resolve/">webpack alias</a>. I know these are quite general statements, so I&#8217;ll show you two examples.</p>



<h3 class="wp-block-heading">Wrong casing in import</h3>



<p>First, we had a problem of <code>webpack watch</code> not refreshing bundles after making changes in a single file. This file is named <code>questionnaireCorrectTemplate.tsx</code>. It exports a single React component called <code>QuestionnaireCorrectTemplate</code>.</p>



<p>In order to use this component, we imported it in another file in the following way:</p>



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



<p>Have you already noticed the problem? Note that the import contains the module name with <strong>capital</strong> letter: &#8220;./<strong>Q</strong>uestionnaireCorrectTemplate&#8221;. As soon as we changed it to a small letter, so exactly as the  <code>questionnaireCorrectTemplate.tsx</code>  file&#8217;s name:</p>



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



<p>Everything started to magically work ? Since this change, <code>webpack watch</code> is properly rebuilding our bundles after changes in the affected file ?</p>



<h3 class="wp-block-heading">Wrong casing in webpack alias</h3>



<p>We had another issue with <code>webpack</code> not refreshing the bundles when we made changes in a <code>Site.css</code> file. This is a file in which we store custom CSS for our React app. The issue was very similar to the previous one. After starting <code>webpack watch</code>, the first modification of <code>Site.css</code> file triggered a proper rebuild of bundles:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="315" src="https://blog.yumasoft.pl/wp-content/uploads/2021/11/4_WebpackCssCorrectRefresh-1024x315.png" alt="webpack watch correctly rebuilding bundles for css file" class="wp-image-840" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/11/4_WebpackCssCorrectRefresh-1024x315.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/4_WebpackCssCorrectRefresh-300x92.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/4_WebpackCssCorrectRefresh-768x236.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/4_WebpackCssCorrectRefresh.png 1114w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<p>However, all subsequent modifications to the <code>Site.css</code> file causes the webpack watch cached issue:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="66" src="https://blog.yumasoft.pl/wp-content/uploads/2021/11/5_WebpackCachedCssIssue-1024x66.png" alt="webpack watch cached issue for css file" class="wp-image-841" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/11/5_WebpackCachedCssIssue-1024x66.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/5_WebpackCachedCssIssue-300x19.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/5_WebpackCachedCssIssue-768x50.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/5_WebpackCachedCssIssue.png 1094w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>The reason was wrong casing again. However, this time we found out that the <code>Site.css</code> file was included as an alias in webpack configuration file:</p>



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



<p>I guess you can already see what is the problem ? The file was included as <code><strong>s</strong>ite.css</code>, instead of <code><strong>S</strong>ite.css</code>. As soon as we changed this part of the <code>webpack.config.js</code> file to the one with proper casing:</p>



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



<p>Everything started to work correctly. Since this change, <code>webpack watch</code> correctly refreshes the bundles after each modification of <code>Site.css</code> file ?</p>



<h2 class="wp-block-heading">Webpack watch cached &#8211; summary</h2>



<p>I hope this article helps you solve your <code>webpack watch</code> refreshing issue ? Webpack is still kind of magical tool for me and it surprises me very often. Such issues can be really painful &#8211; that&#8217;s why I wanted to share our solution to this problem with you. </p>



<p>The general advice is to find how the problematic file is imported/required/added to webpack build flow. It&#8217;s very probably that you have the wrong casing somewhere. I hope it saves you some pain one day!</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/11/webpack-watch-cached-and-not-refreshing-bundles/">Webpack Watch Cached And Not Refreshing Bundles</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/11/webpack-watch-cached-and-not-refreshing-bundles/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How Does TypeScript Types Narrowing Work?</title>
		<link>https://blog.yumasoft.pl/2021/10/how-does-typescript-types-narrowing-work/</link>
					<comments>https://blog.yumasoft.pl/2021/10/how-does-typescript-types-narrowing-work/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Wed, 27 Oct 2021 05:30:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ts]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=736</guid>

					<description><![CDATA[<p>Have you ever heard about TypeScript types narrowing? Have you ever seen such an error in your TypeScript code: Even tough, you really knew that the name property exists on the person object? How did you solve this problem? Did you cast the variable to the expected type? If the answer to at least one&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/10/how-does-typescript-types-narrowing-work/">How Does TypeScript Types Narrowing Work?</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Have you ever heard about <em>TypeScript types narrowing</em>? Have you ever seen such an error in your TypeScript code:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="604" height="118" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/1_TypeScript_missing_property_error.png" alt="TypeScript complains about &quot;name&quot; property not existing  on &quot;person&quot; object of type &quot;object&quot;" class="wp-image-746" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/1_TypeScript_missing_property_error.png 604w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/1_TypeScript_missing_property_error-300x59.png 300w" sizes="auto, (max-width: 604px) 100vw, 604px" /></figure></div>



<p>Even tough, you <em>really knew</em> that the <code>name</code> property exists on the <code>person</code> object? How did you solve this problem? Did you cast the variable to the expected type?</p>



<p>If the answer to at least one of those questions is <em>yes,</em> you might read on. There are better ways of determining the type of local variable in TypeScript. What&#8217;s more, TS compiler does a lot of stuff for us by leveraging types narrowing. In this article, we are going to see how it works and how it can be useful ?</p>



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



<h2 class="wp-block-heading">TypeScript types narrowing &#8211; what is that?</h2>



<p>One of the <a href="https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/">goals of TypeScript</a> is to make writing JavaScript code better and more secure by adding the typing system. However, we don&#8217;t want the typing system to be problematic and making us to use the <em>dirty haxes</em> like all the time casting our variables explicitly, do we? ?</p>



<p>First, we need to understand that TypeScript does a lot of stuff in order to ensure we work with the most specific type as possible. Basically, if the exact type of local variable can be determined, TS compiler tries to do that.</p>



<p>Imagine such a function:</p>



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



<p>In <a href="https://www.javatpoint.com/what-is-vanilla-javascript">vanilla JS</a>, such code would of course compile. But in our case, TypeScript sees a problem:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="730" height="170" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/1_TypeScript_type_error.png" alt="TypeScript complains that &quot;lenght&quot; doesn't exist on a variable of type &quot;string&quot; or &quot;number&quot;" class="wp-image-740" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/1_TypeScript_type_error.png 730w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/1_TypeScript_type_error-300x70.png 300w" sizes="auto, (max-width: 730px) 100vw, 730px" /></figure></div>



<p>The compiler is right here. Our parameter is either <code>string</code> or <code>number</code>, so the <code>length</code> property may not be available if someone passes a <code>number</code>.</p>



<p>You might have already been in such situations. What did you do? Well, sometimes when the code is more complex, and you <em>just know</em> that the variable contains the requested property, it might be tempting to cast:</p>



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



<p>Of course, we will satisfy the compiler here. But does that make our code any more safe than the previous version? Not at all.</p>



<p>In our example, based on the code we have written, TypeScript is not able to determine the exact type of <code>variable</code>. We can help it by using <em>type guards</em>. We will talk about them in details in a separate article. However, for now it&#8217;s enough to know that the language has some built-in type guards &#8211; one of them is <a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#typeof-type-guards">typeof</a>. Let&#8217;s use it in our function:</p>



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



<p>That&#8217;s cool, TS compiler doesn&#8217;t complain anymore. But how does it work? Our code is still accessing <code>variable.length</code> directly, there&#8217;s no casting there.</p>



<p>Notice what your IDE tells you when you hover your mouse over the <code>variable</code> in the first <em>if</em> branch after checking if the type is <code>string</code>:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="559" height="202" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/2_TypeScript_narrowing1.png" alt="TypeScript types narrowing figures out the type of variable is &quot;string&quot;" class="wp-image-741" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/2_TypeScript_narrowing1.png 559w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/2_TypeScript_narrowing1-300x108.png 300w" sizes="auto, (max-width: 559px) 100vw, 559px" /></figure></div>



<p>and also what it tells you when hovering over <code>variable</code> in the <code>else</code> block:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="560" height="178" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/3_TypeScript_narrowing2-1.png" alt="TypeScript types narrowing figures out the type of variable is &quot;number&quot;" class="wp-image-743" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/3_TypeScript_narrowing2-1.png 560w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/3_TypeScript_narrowing2-1-300x95.png 300w" sizes="auto, (max-width: 560px) 100vw, 560px" /></figure></div>



<p>Wow! The IDE magically knows what&#8217;s the exact type of our locally scoped variable! ?</p>



<p>This magic is called <em>types narrowing</em>. TypeScript compiler does many things to ensure that the type of local variable you work with is of the most specific type possible. It is based on the complex analysis of the source code, especially the code surrounding the place in which the local variable is used.</p>



<h2 class="wp-block-heading"><em>in</em> keyword in types narrowing</h2>



<p>Let&#8217;s take a look at this code now:</p>



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



<p>Let&#8217;s say we have a variable which may either be <code>Employee</code> or <code>Manager</code>:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="166" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/5_TypeScript_narrowing_unknown_type-1-1024x166.png" alt="TypeScript doesn't know the type is it's not narrowed" class="wp-image-756" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/5_TypeScript_narrowing_unknown_type-1-1024x166.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/5_TypeScript_narrowing_unknown_type-1-300x49.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/5_TypeScript_narrowing_unknown_type-1-768x125.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/5_TypeScript_narrowing_unknown_type-1.png 1189w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<p>In that place, TypeScript can&#8217;t say whether <code>person</code> is <code>Employee</code> or <code>Manager</code>.</p>



<p>Maybe you&#8217;ve used the <code>in</code> keyword for determining types before. Types narrowing also works with <code>in</code> keyword:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="688" height="228" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/6_TypeScript_narrowing_in_keyword.png" alt="in keyword helps TypeScript narrow the type" class="wp-image-757" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/6_TypeScript_narrowing_in_keyword.png 688w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/6_TypeScript_narrowing_in_keyword-300x99.png 300w" sizes="auto, (max-width: 688px) 100vw, 688px" /></figure></div>



<p>As you can see, TypeScript analyzed the flow, and it noticed that in the first <code>if</code> block we check whether the <code>person</code> object contains the <code>employees</code> property. If it does, the compiler known it must be <code>Manager</code>, not <code>Employee</code>. Cool, isn&#8217;t it? ?</p>



<h2 class="wp-block-heading">Narrowing and equality operator</h2>



<p>Another interesting application of TypeScript types narrowing is using the equality operator:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="780" height="417" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/7_TypeScript_narrowing_equality.png" alt="TypeScript types narrowing determines type of variable based on === equality operator" class="wp-image-760" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/7_TypeScript_narrowing_equality.png 780w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/7_TypeScript_narrowing_equality-300x160.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/7_TypeScript_narrowing_equality-768x411.png 768w" sizes="auto, (max-width: 780px) 100vw, 780px" /></figure></div>



<p>Notice how TypeScript determines the type of variables based on the <code>===</code> operator. If both parameters are equal, they must be both <code>string</code>s.</p>



<h2 class="wp-block-heading">TypeScript types narrowing &#8211; summary</h2>



<p>Narrowing in TypeScript is a very power feature of the language. I just showed you some simple examples, so you can grasp the idea and notice how much TS compiler does to ensure your type is <em>the correct one</em>.</p>



<p>If you&#8217;re interested, you can read much more about this in <a href="https://www.typescriptlang.org/docs/handbook/2/narrowing.html">TypeScript docs</a>. It&#8217;s however worth knowing how TypeScript compiler works and how you should properly check for types in your code. Remember that using <code>any</code> or explicit casting should be your last resort ?</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/10/how-does-typescript-types-narrowing-work/">How Does TypeScript Types Narrowing Work?</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/10/how-does-typescript-types-narrowing-work/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Run Cypress Tests From NUnit Test (C#)</title>
		<link>https://blog.yumasoft.pl/2021/09/run-cypress-tests-from-nunit-test-csharp/</link>
					<comments>https://blog.yumasoft.pl/2021/09/run-cypress-tests-from-nunit-test-csharp/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 13 Sep 2021 13:00:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[end-to-end tests]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[software testing]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=623</guid>

					<description><![CDATA[<p>In one of our projects, we recently started using Cypress for end-to-end testing. Cypress executes tests written in JavaScript or TypeScript. However, we wanted to first perform some data-preparation operations using C#. In fact, we needed to run Cypress tests from NUnit C# tests. In this article, I&#8217;m sharing how this can be done ?&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/09/run-cypress-tests-from-nunit-test-csharp/">Run Cypress Tests From NUnit Test (C#)</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In one of our projects, we recently started using <a href="https://www.cypress.io/">Cypress</a> for end-to-end testing. Cypress executes tests written in JavaScript or TypeScript. However, we wanted to first perform some data-preparation operations using C#. In fact, we needed to run Cypress tests from NUnit C# tests.</p>



<p>In this article, I&#8217;m sharing how this can be done ? It will also be useful if you use another testing framework than NUnit, but the examples are based on it.</p>



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



<h2 class="wp-block-heading">Starting point</h2>



<p>You can find the code accompanying this article here: <a href="https://github.com/dsibinski/cypress-csharp-example">https://github.com/dsibinski/cypress-csharp-example</a></p>



<p>Our starting point is a React application with Cypress tests:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="888" height="332" src="https://blog.yumasoft.pl/wp-content/uploads/2021/09/1_WebAppCypressTest.png" alt="React app with Cypress test" class="wp-image-628" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/09/1_WebAppCypressTest.png 888w, https://blog.yumasoft.pl/wp-content/uploads/2021/09/1_WebAppCypressTest-300x112.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/09/1_WebAppCypressTest-768x287.png 768w" sizes="auto, (max-width: 888px) 100vw, 888px" /></figure></div>



<p>I assume that your tests specs are placed within <code>cypress/integration</code> folder. The only spec we have is called <code>submit_spec.ts</code> and it placed in a subfolder called <code>form</code>.</p>



<p>Now imagine, that your backend is written in C#. Maybe you&#8217;d like to perform some data initialization <em>before </em>Cypress tests are executed.</p>



<p>In our case, we have a C# backend, and we&#8217;ve already had a mechanism for running integration tests. This mechanism runs our server app with an in-memory database, which makes the maintenance of tests very easy.</p>



<p>We wanted to use the same mechanism for our new, E2E Cypress tests. It means that we would be able to write some C# initialization code (data creation mostly) and then run Cypress tests from NUnit test.</p>



<p>What we want to achieve is to have a C#-Cypress NUnit test look as follows:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="382" height="162" src="https://blog.yumasoft.pl/wp-content/uploads/2021/09/2_RunningCypressTestFromCSharpIdea.png" alt="The picture shows how to run cypress tests from nunit by only calling RunCypressTest(&quot;form/submit_spec.ts&quot;) - the method we will add soon" class="wp-image-630" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/09/2_RunningCypressTestFromCSharpIdea.png 382w, https://blog.yumasoft.pl/wp-content/uploads/2021/09/2_RunningCypressTestFromCSharpIdea-300x127.png 300w" sizes="auto, (max-width: 382px) 100vw, 382px" /></figure></div>



<p>Let&#8217;s now see how we can implement the <code>RunCypressTest</code> method ?</p>



<h2 class="wp-block-heading">Implementing a method to run Cypress tests from NUnit</h2>



<p>To make things easier, let&#8217;s create a base class all our test classes can use. We&#8217;ll call it <code>CypressTestsBase</code> and add a <code>RunCypressTest</code> method to it: </p>



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



<p>This code does the job. Few interesting parts:</p>



<ul class="wp-block-list"><li>Line 12 &#8211; I&#8217;m getting there the path to the web app workspace where <code>npx cypress run</code> command can be executed. It might be more complicated in your scenario, so you should adjust accordingly here</li><li>Lines 13-20: thanks to setting <code>RedirectStandardInput</code> to <code>true</code>, we can later execute a custom command via <code>cmd</code>; setting <code>RedirectStandardOutput</code> to <code>true</code> allows us to write everything <code>npx cypress run</code> command does to NUnit test&#8217;s output</li><li>Line 28: <code>TestContext.Out.WriteLine(cypressProcessOutput);</code> actually writes the Cypress output to NUnit test result</li><li>Line 29: here we handle errors during Cypress execution. <code>npx cypress run</code> executed via <code>cmd</code> will return a code different from 0 if anything goes wrong.</li></ul>



<p>We can now change <code>FormTests</code> class to run its corresponding Cypress test:</p>



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



<p>Finally, it allows to run our C# test, which then runs its corresponding Cypress spec and prints the output:</p>



<div class="wp-block-image"><figure class="aligncenter size-full is-resized"><img loading="lazy" decoding="async" src="https://blog.yumasoft.pl/wp-content/uploads/2021/09/3_CypressTestResultInNUnitResult-1.png" alt="Run Cypress tests from NUnit - sample passed test in JetBrains Rider" class="wp-image-638" width="706" height="307" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/09/3_CypressTestResultInNUnitResult-1.png 706w, https://blog.yumasoft.pl/wp-content/uploads/2021/09/3_CypressTestResultInNUnitResult-1-300x130.png 300w" sizes="auto, (max-width: 706px) 100vw, 706px" /></figure></div>



<h2 class="wp-block-heading">Cypress errors in NUnit result</h2>



<p>Let&#8217;s see what happens if something is broken on Cypress/web app&#8217;s side. Let&#8217;s run our test, but with our web app not running.</p>



<p>The NUnit test fails, and we can clearly see what was wrong:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="999" height="323" src="https://blog.yumasoft.pl/wp-content/uploads/2021/09/4_CypressTestFailedNUnitResult.png" alt="Run Cypress tests from NUnit - failed test in JetBrains Rider, all details from cypress visible" class="wp-image-640" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/09/4_CypressTestFailedNUnitResult.png 999w, https://blog.yumasoft.pl/wp-content/uploads/2021/09/4_CypressTestFailedNUnitResult-300x97.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/09/4_CypressTestFailedNUnitResult-768x248.png 768w" sizes="auto, (max-width: 999px) 100vw, 999px" /></figure></div>



<p>You will see similar errors if something goes wrong in a Cypress test. Cypress also <a href="https://docs.cypress.io/guides/guides/screenshots-and-videos">saves compressed videos and screenshots</a>, so if something goes wrong, diagnosing such failed tests is very easy.</p>



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



<p>I hope you found this article on how to run Cypress tests from NUnit useful. Remember that it not only works with NUnit &#8211; it will be suitable for any testing framework. You might need to adjust some parts of the solution, e.g. printing the results to the test result&#8217;s output.</p>



<p>This solution allowed us to improve our testing by adding a new type of tests to our <a href="https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/">pipeline</a>. Right next to <a href="https://blog.yumasoft.pl/2021/07/why-naming-tests-matters/">proper naming</a> and <a href="https://blog.yumasoft.pl/2021/08/make-your-tests-bulletproof-with-mutation-testing/">mutation testing</a>, E2E tests add another layer of security and improve the quality of our software.</p>



<p>Once more, you can find the complete source code <a href="https://github.com/dsibinski/cypress-csharp-example">here</a>. If you want, feel free to copy it and change it according to your needs ?</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/09/run-cypress-tests-from-nunit-test-csharp/">Run Cypress Tests From NUnit Test (C#)</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/09/run-cypress-tests-from-nunit-test-csharp/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>How To Fix JSON.NET Circular References ($ref) In JavaScript</title>
		<link>https://blog.yumasoft.pl/2021/08/how-to-fix-json-net-circular-references-ref-in-javascript/</link>
					<comments>https://blog.yumasoft.pl/2021/08/how-to-fix-json-net-circular-references-ref-in-javascript/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Sun, 15 Aug 2021 07:30:27 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json.net]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=518</guid>

					<description><![CDATA[<p>If you work with a .NET backend that uses JSON.NET for serialization, you might encounter such weird things in the JSON result returned from a controller: But what you actually want is the real object present there: After reading this article, you will know how to achieve that ? JSON.NET circular references are the issue&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/08/how-to-fix-json-net-circular-references-ref-in-javascript/">How To Fix JSON.NET Circular References ($ref) In JavaScript</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>If you work with a .NET backend that uses <a href="https://www.newtonsoft.com/json">JSON.NET</a> for serialization, you might encounter such weird things in the JSON result returned from a controller:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="373" height="311" src="https://blog.yumasoft.pl/wp-content/uploads/2021/08/1_CircularReferenceExample.png" alt="JSON result from ASP.NET controller with $ref: &quot;1&quot; instead of a real object. Example of JSON.NET circular references" class="wp-image-519" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/08/1_CircularReferenceExample.png 373w, https://blog.yumasoft.pl/wp-content/uploads/2021/08/1_CircularReferenceExample-300x250.png 300w" sizes="auto, (max-width: 373px) 100vw, 373px" /></figure></div>



<p>But what you actually want is the real object present there:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="560" height="323" src="https://blog.yumasoft.pl/wp-content/uploads/2021/08/3_CurcularReferencesRemoved-1.png" alt="JSON result from ASP.NET controller with $ref: &quot;1&quot; replaced with a real object's content" class="wp-image-535" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/08/3_CurcularReferencesRemoved-1.png 560w, https://blog.yumasoft.pl/wp-content/uploads/2021/08/3_CurcularReferencesRemoved-1-300x173.png 300w" sizes="auto, (max-width: 560px) 100vw, 560px" /></figure></div>



<p>After reading this article, you will know how to achieve that ?</p>



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



<h2 class="wp-block-heading">JSON.NET circular references are the issue</h2>



<p>Let&#8217;s take a look at our problematic JSON response object once again:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="373" height="311" src="https://blog.yumasoft.pl/wp-content/uploads/2021/08/1_CircularReferenceExample.png" alt="JSON result from ASP.NET controller with $ref: &quot;1&quot; instead of a real object" class="wp-image-519" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/08/1_CircularReferenceExample.png 373w, https://blog.yumasoft.pl/wp-content/uploads/2021/08/1_CircularReferenceExample-300x250.png 300w" sizes="auto, (max-width: 373px) 100vw, 373px" /></figure></div>



<p><code>$ref: "1"</code> is basically a reference to another object, which has already been serialized. In that case, the <code>BestFriend</code> field references the first element of the result array with <code>$id: "1"</code>. It turns out that these are <strong>JSON.NET circular references</strong>. However, you don&#8217;t want these references in your client-side JS code &#8211; you need the actual objects to work with.</p>



<p>I struggled with this issue some time ago in my JavaScript code. In the beginning, I had no idea it might come from the fact that JSON.NET is used for serialization (thanks to our colleague Rafał for pointing that out to me ?). Let&#8217;s see what exactly happens here and why.</p>



<h2 class="wp-block-heading">JSON.NET serialization</h2>



<p>The JS example presented above shows a JSON result received from the following ASP.NET controller method:</p>



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



<p>This is the full content of <code>json</code> string after serialization:</p>



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



<p>Of course, this is great that JSON.NET does that. Why would we repeat the same objects multiple times in JSONs? The optimization here is that the same objects are serialized once and referenced from all places that use them.</p>



<p>However, if such JSONs are sent to the browser, fields with refs are of no use. We need the actual objects to be there, so we can process the results correctly using JavaScript.</p>



<h2 class="wp-block-heading">Solution 1: fix the issue by reconfiguring JSON.NET serializer</h2>



<p>The first obvious solution could be to configure JSON.NET to not use its optimization for references. Quickly searching through its documentation, we find that <a href="https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_PreserveReferencesHandling.htm">PreserveReferencesHandling</a> setting is responsible for that.</p>



<p>If we change our controller&#8217;s code and serialize objects with <code>PreserveReferencesHandling</code> set to <code>None</code>:</p>



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



<p>Our JSON file will look exactly as we&#8217;d expect:</p>



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



<p>However, why would we disable JSON.NET circular references? This optimization can sometimes save us huge amounts of data to be sent over the wire. If you have thousands of objects, it might not make sense in sending them all to the client-side code.</p>



<p>You may also have no control of the backend/API you receive the data from. It might as well be dangerous to change the JSON serialization settings, especially if you work on a big project.</p>



<p>If reconfiguration of JSON.NET circular references serialization is not possible in your case, see the other solution (which we also use).</p>



<h2 class="wp-block-heading">Solution 2: resolve circular references using JavaScript</h2>



<p>So we are back to this state in JavaScript:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="373" height="311" src="https://blog.yumasoft.pl/wp-content/uploads/2021/08/1_CircularReferenceExample.png" alt="JSON result from ASP.NET controller with $ref: &quot;1&quot; instead of a real object" class="wp-image-519" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/08/1_CircularReferenceExample.png 373w, https://blog.yumasoft.pl/wp-content/uploads/2021/08/1_CircularReferenceExample-300x250.png 300w" sizes="auto, (max-width: 373px) 100vw, 373px" /></figure></div>



<p>What we want is to have the full objects&#8217; contents instead of <code>$ref</code> elements in our JSON result.</p>



<p>In fact, all the data we need is already there. The first element of the array with <code>$id: "1"</code> is object that should be put in place of all <code>$ref: "1"</code> references.</p>



<p>As you can imagine, this requires a mechanism that would traverse the results tree and replace all these values according to IDs of the referenced objects.</p>



<p>Fortunately, we don&#8217;t need to implement it ourselves. There are few implementations of such a tool, circulating the web.</p>



<p>We use something called <code>JsonNetDecycle</code> which we found few years back already. I noticed there&#8217;s a new version of it ported to TypeScript: <a href="https://github.com/jmackay/angular2-demoproject-aspnetcore/blob/master/Source/Angular2Demo.Web/wwwroot/lib/jsonNetDecycle.ts">jsonNetDecycle.ts</a>. It doesn&#8217;t provide full type support as it is a few years old already, and I didn&#8217;t try it. But you can if you use <a href="https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/">TypeScript</a> ? There&#8217;s also a <code>cycle.js</code> solution available <a href="https://github.com/douglascrockford/JSON-js/blob/master/cycle.js">here</a> and recommended by many folks. I believe it may also solve our issue, but again &#8211; this is not what we are using.</p>



<p>The version of the  <code>JsonNetDecycle</code>  we use is <a href="https://gist.github.com/johnbabb/2d69374455e386eae87a">this one</a>. Simply create a new file called <code>JsonNetDecycle.js</code> in your project and copy this Gist&#8217;s contents to this file. You can skip the last part starting with <code>(function(console)</code> (line 112) &#8211; we won&#8217;t need it.</p>



<p>Now, we can use the exported <code>JsonNetDecycle.retrocycle()</code> function to &#8220;fix&#8221; our JSON:</p>



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



<p>Let&#8217;s see whether this works.</p>



<p>This is what we get from the API before resolving JSON.NET circular references:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="593" height="311" src="https://blog.yumasoft.pl/wp-content/uploads/2021/08/2_CircularReferencesExampleWithDetails.png" alt="JSON result from ASP.NET controller with $ref: &quot;1&quot; instead of a real object" class="wp-image-532" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/08/2_CircularReferencesExampleWithDetails.png 593w, https://blog.yumasoft.pl/wp-content/uploads/2021/08/2_CircularReferencesExampleWithDetails-300x157.png 300w" sizes="auto, (max-width: 593px) 100vw, 593px" /></figure></div>



<p>So we still have references there instead of real objects.</p>



<p>Now let&#8217;s see what happens with the <code>result</code> object after executing <code>JsonNetDecycle.retrocycle()</code> on it:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="560" height="323" src="https://blog.yumasoft.pl/wp-content/uploads/2021/08/3_CurcularReferencesRemoved.png" alt="JSON result from ASP.NET controller using JSON.NET without JSON.NET circular references" class="wp-image-533" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/08/3_CurcularReferencesRemoved.png 560w, https://blog.yumasoft.pl/wp-content/uploads/2021/08/3_CurcularReferencesRemoved-300x173.png 300w" sizes="auto, (max-width: 560px) 100vw, 560px" /></figure></div>



<p>Wohhooa! That&#8217;s what we wanted ? There are no circular references in our JSON response anymore!</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/08/how-to-fix-json-net-circular-references-ref-in-javascript/">How To Fix JSON.NET Circular References ($ref) In JavaScript</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/08/how-to-fix-json-net-circular-references-ref-in-javascript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 loading="lazy" 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="auto, (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 loading="lazy" 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="auto, (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 loading="lazy" 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="auto, (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>
		<item>
		<title>Migrating JavaScript React App to TypeScript</title>
		<link>https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/</link>
					<comments>https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 28 Jun 2021 07:15:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[javascript to typescript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[react]]></category>
		<category><![CDATA[reactjs]]></category>
		<category><![CDATA[ts]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=342</guid>

					<description><![CDATA[<p>In one of our biggest projects, we develop and maintain a React web application. Last year, we decided to migrate it to TypeScript. How did it go? How did we migrate JavaScript to TypeScript? Was it worth it? What struggles we met and are we still having any issues? I&#8217;ll try to address these questions&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/">Migrating JavaScript React App to TypeScript</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In one of our biggest projects, we develop and maintain a React web application. Last year, we decided to migrate it to TypeScript. How did it go? How did we migrate JavaScript to TypeScript? Was it worth it? What struggles we met and are we still having any issues? I&#8217;ll try to address these questions in this article.</p>



<p><em>Disclaimer: I&#8217;m not describing the migration process step-by-step or TypeScript itself in this article. You can find many resources on that online, including <a href="https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html">official TS docs</a>.</em></p>



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



<h2 class="wp-block-heading">JavaScript to TypeScript migration strategy</h2>



<p>If you search online, you will find many possible strategies of migrating JavaScript to TypeScript. We considered two of them:</p>



<ul class="wp-block-list"><li>converting the whole JavaScript codebase to TypeScript at once</li><li>starting to use TypeScript compiler and migrate step-by-step.</li></ul>



<p>After a short discussion, we decided to go with the <strong>step-by-step migration strategy</strong>. Why? First, it&#8217;s less time-consuming. Today, we know that it would kill us if we decided to convert all our JS codebase to TS at once. It would also freeze our codebase for a while &#8211; until every <code>.JS(X)</code> file is renamed to <code>.TS(X)</code> and all compile errors are fixed.</p>



<p>Thanks to the fact that TypeScript is a superset of JavaScript, <code>.JS(X)</code> and <code>.TS(X)</code> files can co-exist even if you switch to use TypeScript compiler. That&#8217;s because <strong>every JavaScript is also a valid TypeScript code</strong> and TS compiler can handle it.</p>



<p>That&#8217;s very comfortable. Just think about that &#8211; <strong>you can switch your project to TypeScript without actually having to change any code</strong>. We decided to immediately re-write a few of our recently-added functionalities to TypeScript and convert the rest gradually.</p>



<h2 class="wp-block-heading">What TypeScript gave us</h2>



<p>Before I tell you about the struggles, I want to really <strong>encourage you to migrate JavaScript to TypeScript</strong>. After 1 year since we switched to TypeScript, I&#8217;m telling you <strong>it&#8217;s the best thing you can do to your web app</strong> ?</p>



<p>Let&#8217;s see what you&#8217;ll gain if you decide to migrate JavaScript to TypeScript.</p>



<h3 class="wp-block-heading">Types in new code</h3>



<p>As soon as you switch your compiler to <em>ts-loader</em>, you can start creating files with TypeScript extensions (<code>.TS</code>, <code>.TSX</code>). It means that all your new code can now leverage types:</p>



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



<p>Enjoy ?</p>



<h3 class="wp-block-heading">Typing information for existing libraries</h3>



<p>Have you been wondering what about the external libraries you are already using? How to get types for them? I was thinking exactly about this before migration.</p>



<p>It turns out that after you migrate JavaScript to TypeScript, most of the <code>npm</code> packages you use already have built-in typing information. You can check it on your package&#8217;s <code>npm</code> page:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="804" height="158" src="https://blog.yumasoft.pl/wp-content/uploads/2021/06/npm_typescript_icon.png" alt="" class="wp-image-350" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/06/npm_typescript_icon.png 804w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/npm_typescript_icon-300x59.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/npm_typescript_icon-768x151.png 768w" sizes="auto, (max-width: 804px) 100vw, 804px" /></figure></div>



<p>If it doesn&#8217;t, you can try installing <code>npm</code> package called &#8220;@types/your_library_name&#8221;, e.g. <code>npm i @types/backbone</code>.</p>



<p>As a last resort, you can <a href="https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html">create the declaration files yourself</a>.</p>



<h3 class="wp-block-heading">JavaScript-TypeScript coexistence</h3>



<p>A huge benefit of TypeScript is that JavaScript code can co-exist with it. If you are like we were a year ago, with a lot of legacy JavaScript code and are afraid of switching to TS &#8211; don&#8217;t be. TypeScript makes it very easy to maintain your JS codebase or even mix it altogether.</p>



<p>There are no issues in using JavaScript code in TypeScript and otherwise. You can even <a href="https://blog.yumasoft.pl/2021/06/how-to-use-javascript-component-in-react-component/">use JS components in TypeScript</a>. We used to have a <code>services.js</code> file where we kept &#8220;services&#8221; exported as follows:</p>



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



<p>After migrating it to TypeScript (meaning renaming to <code>services.ts</code>) it turns out that these two definitions can co-exist, even in the same file:</p>



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



<p>As you can see, we added typing information to the legacy <code>StockService</code> as well. That&#8217;s because we have <code>noImplicitAny</code> enabled in our <a href="https://www.typescriptlang.org/tsconfig/#noImplicitAny">TypeScript configuration</a>. It doesn&#8217;t allow leaving any variables which are of <code>any</code> type. In any case, if you don&#8217;t know what type to use for a given variable/function, you can always type it explicitly as <code>any</code>, while moving the new code to fully-typed equivalents. That&#8217;s the power of TypeScript ?</p>



<h3 class="wp-block-heading">Compile errors</h3>



<p>I should have started with that! It might be obvious, but not for people who only worked with JavaScript on the frontend before ? Sure, you could get suggestions and warnings with <a href="https://eslint.org/">ESLint</a>, but that&#8217;s not the same as really enforced types checking on compilation level:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="804" height="141" src="https://blog.yumasoft.pl/wp-content/uploads/2021/06/TypeScript_webpack_compilation_error.png" alt="" class="wp-image-358" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/06/TypeScript_webpack_compilation_error.png 804w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/TypeScript_webpack_compilation_error-300x53.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/TypeScript_webpack_compilation_error-768x135.png 768w" sizes="auto, (max-width: 804px) 100vw, 804px" /></figure></div>



<h3 class="wp-block-heading">Many TS compiler options</h3>



<p>TypeScript compiler has many useful <a href="https://www.typescriptlang.org/tsconfig#noImplicitAny">configuration options</a>. If you want to support JavaScript alongside TypeScript, you should enable <code>allowJs</code>. We have already mentioned the <code>noImplicitAny</code> setting that forces you to always explicitly specify types in your code. There&#8217;s much more stuff helpful in various scenarios of JS-TS migration. We were having some issues with imports from modules without default exports &#8211; enabling <code>allowSyntheticDefaultImports</code> saved us there.</p>



<p>What I&#8217;d like to emphasize here is that TypeScript compiler is really helpful ? It allows you to set your &#8220;typing&#8221; level by enforcing less or more rules. Thanks to that, you can slowly move towards more strict type checking rules like <code>noImplicitAny</code>. You&#8217;re not forced to do that from the beginning.</p>



<h3 class="wp-block-heading">Better coding experience</h3>



<p>This is definitely the best thing about TypeScript. It just feels more right to use it than coding in plain JavaScript. I see no reason to start a new project with vanilla JS today. I&#8217;d say even more &#8211; I see no reason to not migrate your JavaScript app to TypeScript. The sooner, the better!</p>



<p>You can catch your errors earlier. No need to debug the code to see if the property you used actually exists on an object. Your IDE gives you code completion suggestions. It&#8217;s a pure pleasure ?</p>



<h2 class="wp-block-heading">Migration challenges</h2>



<p>Of course, we met some challenges during migration. I don&#8217;t call them issues, because they are all manageable. Many people have already done those things, so you can find solutions to almost any issue online. Let&#8217;s see what we had to overcome.</p>



<h3 class="wp-block-heading">webpack configuration</h3>



<p>If there&#8217;s something I don&#8217;t like about web development, it&#8217;s definitely <a href="https://webpack.js.org/">webpack.</a>.. This is a typical &#8220;fighting with machines&#8221; case. Poor documentation, lack of well-described solutions online and some specificity in every project.</p>



<p>We had problems with <code>babel-loader</code> which we used to transpile JavaScript files before switching to TypeScript. Initially, afraid of introducing a regression, we wanted to keep <code>babel-loader</code> for legacy JS files. We couldn&#8217;t make it work, so we decided to switch completely to <code>ts-loader</code> for all files (including JavaScript ones).</p>



<p>Next issue was a lack of proper source maps and problems in debug mode. The breakpoints were often not hit. After a bit of searching and trial-error sessions, we found a solution. It was necessary to modify <code>webpack.config.js</code> and set <code>devtool</code> webpack setting to <code>eval-source-map</code>. We also had to use <a href="https://www.npmjs.com/package/source-map-loader">source-map-loader</a> for JavaScript files. Finally, this webpack configuration solved the debugging problems:</p>



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



<p>You need to be really patient with webpack&#8230; But if you have already worked with it, I guess you know that very well ?</p>



<h3 class="wp-block-heading">Adding types to legacy JS code</h3>



<p>Adding types information with <code>noImplicitAny</code> set to <code>true</code> is sometimes challenging. Especially if you have some bigger JS files with no typing information at all. Sometimes you need to search for function&#8217;s calls to find out of what types are its parameters. In other cases, you might need to run your app and see that at runtime ? </p>



<p>Fortunately, as we already said, TypeScript doesn&#8217;t force you doing that. You can play with <a href="https://www.typescriptlang.org/tsconfig#noImplicitAny">tsconfig</a> or use <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-6.html#suppress-errors-in-ts-files-using--ts-ignore-comments">@ts-ignore</a> as a last resort. </p>



<h3 class="wp-block-heading">Handling libraries without types</h3>



<p>Some of the libraries we used don&#8217;t provide any typing information. It makes working with them a bit difficult, especially if your TS compiler is already configured in a bit stricter mode. In such case, you might get an error similar to the following one:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="184" src="https://blog.yumasoft.pl/wp-content/uploads/2021/06/formio_missing_types-1024x184.png" alt="" class="wp-image-365" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/06/formio_missing_types-1024x184.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/formio_missing_types-300x54.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/formio_missing_types-768x138.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/formio_missing_types.png 1062w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<p>Your options here are to either write the declaration files yourself, or import the library differently. You can, for instance, <code>require</code> it:</p>



<pre class="wp-block-code"><code>const FormIO = require('react-formio');</code></pre>



<p>and then use this <code>FormIO</code> variable which is typed as <code>any</code>. Again &#8211; that works because of TypeScript&#8217;s flexibility, but might not be very comfortable to live with.</p>



<h2 class="wp-block-heading">Migrate JavaScript to TypeScript &#8211; should I do it?</h2>



<p>Of course! There&#8217;s only one right answer. Don&#8217;t wait too long &#8211; start being more productive with TypeScript as soon as you can ? Believe me &#8211; you will not regret it.</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/">Migrating JavaScript React App to 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/06/migrating-javascript-react-app-to-typescript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Use JavaScript Component in React Component</title>
		<link>https://blog.yumasoft.pl/2021/06/how-to-use-javascript-component-in-react-component/</link>
					<comments>https://blog.yumasoft.pl/2021/06/how-to-use-javascript-component-in-react-component/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 14 Jun 2021 10:27:10 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[react]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=266</guid>

					<description><![CDATA[<p>We recently run into a problem of having to display a vanilla JavaScript component in a React component. This component exposes a static method to render itself, like many of pure JS components do. In a &#8220;normal&#8221; JavaScript context it would be very easy to just call such a method, providing a DOM element to&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/06/how-to-use-javascript-component-in-react-component/">How to Use JavaScript Component in React Component</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>We recently run into a problem of having to display a vanilla JavaScript component in a React component. This component exposes a static method to render itself, like many of pure JS components do. In a &#8220;normal&#8221; JavaScript context it would be very easy to just call such a method, providing a DOM element to render itself into. However, when you want to render such a document as a part of your React component, this becomes a bit tricky. Read on to see how we solved this issue.</p>



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



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



<p>In our case, the component we wanted to render was <a href="https://ckeditor.com/docs/ckeditor5/latest/index.html" target="_blank" rel="noreferrer noopener">CKEditor5</a>. It&#8217;s a tiny and simple WYSIWYG editor.</p>



<p>Its documentation gives the following <a href="https://ckeditor.com/docs/ckeditor5/latest/api/module_editor-classic_classiceditor-ClassicEditor.html#static-function-create" target="_blank" rel="noreferrer noopener">example</a> on how to render the editor using a static <code>create</code> function:</p>



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



<p>In our application, we have a React function component:</p>



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



<p>The goal is to render the <code>ckeditor</code> component within the <code>div</code> with id <code>my-ckeditor</code>.</p>



<h2 class="wp-block-heading">Can I call the component&#8217;s <em>create</em> function in React component?</h2>



<p>The first idea that came to my mind was to somehow call the static<strong> </strong><code>create</code><strong> </strong>function from <code>ckeditor</code> providing <code>my-ckeditor</code> as DOM element&#8217;s id. However, it turns out that we cannot do it directly in the function component before returning, because our <strong>div</strong> is not loaded into the DOM yet at this moment.</p>



<p>Fortunately, there&#8217;s another, easy solution ?</p>



<h2 class="wp-block-heading">React on DOM element being mounted with <strong><em>useCallback</em></strong></h2>



<p>What we basically want to achieve is to execute the<strong> </strong><code>create</code> static function provided by <code>ckeditor</code> when our <code>div</code> is mounted into the DOM.</p>



<p>Let&#8217;s first install the ckeditor5 classic editor <a href="https://www.npmjs.com/package/@ckeditor/ckeditor5-build-classic" target="_blank" rel="noreferrer noopener">npm package</a>:</p>



<pre class="wp-block-code"><code>npm i @ckeditor/ckeditor5-build-classic</code></pre>



<p>Then require <code>ckeditor.js</code> file as <code>Ckeditor</code> in the<strong> .tsx </strong>file with our component:</p>



<pre class="wp-block-code"><code>const Ckeditor = require('@ckeditor/ckeditor5-build-classic/build/ckeditor.js');</code></pre>



<p>Now, instead of assigning an <code>id</code> to our <code>div</code> element, let&#8217;s use the <code>React.useCallback</code> hook and assign it to <code>div</code>&#8216;s <code>ref</code>:</p>



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



<p>Our callback function will be called after our <code>div</code> component mounts (or unmounts). That&#8217;s why we check whether the <code>node</code> is defined, and then we can do something with that.</p>



<p>This is how it looks in debug mode:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="804" height="256" src="https://blog.yumasoft.pl/wp-content/uploads/2021/06/JS_component_in_React_final_result.png" alt="" class="wp-image-268" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/06/JS_component_in_React_final_result.png 804w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/JS_component_in_React_final_result-300x96.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/JS_component_in_React_final_result-768x245.png 768w" sizes="auto, (max-width: 804px) 100vw, 804px" /></figure>



<p>As you can see, <code>node</code> is defined, and it&#8217;s a <code>div</code>. Exactly the DOM element in which we want to render our <code>ckeditor</code> component. Let&#8217;s now simply change <code>console.log</code> to rendering the editor as per documentation:</p>



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



<p>Thanks to that, we can see our editor rendered on the page:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="981" height="348" src="https://blog.yumasoft.pl/wp-content/uploads/2021/06/Ckeditor_rendered_on_React_page.png" alt="" class="wp-image-273" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/06/Ckeditor_rendered_on_React_page.png 981w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/Ckeditor_rendered_on_React_page-300x106.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/Ckeditor_rendered_on_React_page-768x272.png 768w" sizes="auto, (max-width: 981px) 100vw, 981px" /></figure>



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



<p>We often use external components in our projects at Yumasoft. In case of above-mentioned <code>ckeditor</code>, we couldn&#8217;t use <a href="https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/frameworks/react.html" target="_blank" rel="noreferrer noopener">its React component</a> (because of various reasons) and we had to render it statically using JS function. The solution with <code>useCallback</code> turned out to be very useful here. I hope you also find it helpful one day if you ever need to use Javascript component in React ?</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/06/how-to-use-javascript-component-in-react-component/">How to Use JavaScript Component in React Component</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/06/how-to-use-javascript-component-in-react-component/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How To Replace npm Library With Your Own Implementation?</title>
		<link>https://blog.yumasoft.pl/2021/05/how-to-replace-npm-library-with-your-own-implementation/</link>
					<comments>https://blog.yumasoft.pl/2021/05/how-to-replace-npm-library-with-your-own-implementation/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Wed, 05 May 2021 12:51:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[npm]]></category>
		<guid isPermaLink="false">http://blog.yumasoft.pl/?p=20</guid>

					<description><![CDATA[<p>In one of our projects at Yumasoft we needed to replace npm library&#8217;s implementation with a custom one. This was needed because one of the components we use created their own version (fork) of this npm package (the component uses this library internally). In order to make the component working correctly, we had to use&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/05/how-to-replace-npm-library-with-your-own-implementation/">How To Replace npm Library With Your Own Implementation?</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="has-black-color has-text-color">In one of our projects at Yumasoft we needed to replace npm library&#8217;s implementation with a custom one. This was needed because one of the components we use created their own version (fork) of this npm package (the component uses this library internally). In order to make the component working correctly, we had to use the component&#8217;s authors version of the library, instead of using the original npm package. It turned out to not be an easy task, but finally we found a nice and easy solution.</p>



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



<h2 class="wp-block-heading">The problem</h2>



<p>Let&#8217;s say that we install&nbsp;<em>@ckeditor/ckeditor5-build-classic</em>&nbsp;npm package to our web app. This is a kind of package that has many forks. Some companies or individuals fork&nbsp;<a href="https://github.com/ckeditor/ckeditor5-build-classic" target="_blank" rel="noreferrer noopener">ckeditor5-build-classic repository</a>&nbsp;and create their own versions of it, adjusted for use with their products.</p>



<p>The problem is that if you use such a product that has their own fork of&nbsp;<em>@ckeditor/ckeditor5-build-classic</em>, you might want to use this fork as npm package, not the&nbsp;<a href="https://www.npmjs.com/package/@ckeditor/ckeditor5-build-classic" target="_blank" rel="noreferrer noopener">original npm library</a>. However, in most cases there&#8217;s no npm package published for the forked repository.</p>



<p>We need a way to replace the contents (source code) of the original npm library with contents (source code) from the forked library (custom version). Let&#8217;s see how to replace npm library with our own code.</p>



<h2 class="wp-block-heading">The solution</h2>



<p>The first idea that comes to mind could be to create a modified&nbsp;<a href="https://docs.npmjs.com/creating-and-publishing-private-packages" target="_blank" rel="noreferrer noopener">private npm package</a>&nbsp;and use it in our project. However, it requires some maintenance and forking the original library. We found a better solution.</p>



<p>If we go to&nbsp;<em>node_modules</em>&nbsp;folder looking&nbsp;<em>@ckeditor/ckeditor5-build-classic</em>&nbsp;package&#8217;s source, we can see that its source files (bundles) are present in&nbsp;<em>node_modules\@ckeditor\ckeditor5-build-classic\build\</em>&nbsp;folder:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="700" height="273" src="http://blog.yumasoft.pl/wp-content/uploads/2021/05/Ckeditor_node_modules_content.png" alt="" class="wp-image-21" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/05/Ckeditor_node_modules_content.png 700w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/Ckeditor_node_modules_content-300x117.png 300w" sizes="auto, (max-width: 700px) 100vw, 700px" /></figure></div>



<p>If you check some forks of this library on GitHub, there are also bundles present there, e.g.&nbsp;<a href="https://github.com/formio/ckeditor5-build-classic/tree/master/build" target="_blank" rel="noreferrer noopener">here</a>&nbsp;or&nbsp;<a href="https://github.com/freeshineit/ckeditor5-build-classic/tree/master/dist" target="_blank" rel="noreferrer noopener">here</a>. Let&#8217;s say that we need to take the forked version of&nbsp;<em>ckeditor.js</em>&nbsp;file and replace this file in our original npm library. How do we do that?</p>



<h3 class="wp-block-heading">patch-package</h3>



<p>To achieve this, we need to install&nbsp;<a href="https://www.npmjs.com/package/patch-package" target="_blank" rel="noreferrer noopener">patch-package</a>&nbsp;npm package. This is an awesome, life-saving tool created just for what we need. Kudos to our teammate Przemek who recommended this gold tool to me ?</p>



<p>After installing the package with&nbsp;<strong>npm i patch-package</strong>, we can simply go to our npm library&#8217;s source and modify it. In our case, we replace&nbsp;<em>node_modules\@ckeditor\ckeditor5-build-classic\build\ckeditor.js</em>&nbsp;file contents with the contents of&nbsp;<em>ckeditor.js</em>&nbsp;file from our forked library (for example with&nbsp;<a href="https://github.com/formio/ckeditor5-build-classic/blob/master/build/ckeditor.js" target="_blank" rel="noreferrer noopener">this one</a>). After that, we save the file.</p>



<p>The last step is to generate a patch for our original npm library. We do it by executing the following command:&nbsp;<strong>npx patch-package @ckeditor/ckeditor5-build-classic</strong>&nbsp;(replace&nbsp;<strong>@ckeditor/ckeditor5-build-classic</strong>&nbsp;with your package name). This is how it looks:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="854" height="206" src="http://blog.yumasoft.pl/wp-content/uploads/2021/05/npx_patch-package_output.png" alt="" class="wp-image-22" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/05/npx_patch-package_output.png 854w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/npx_patch-package_output-300x72.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/npx_patch-package_output-768x185.png 768w" sizes="auto, (max-width: 854px) 100vw, 854px" /></figure></div>



<p>We can now see that a new folder called&nbsp;<em>patches</em>&nbsp;was created in our web app&#8217;s directory. In our case, it contains only a single patch file:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="859" height="136" src="http://blog.yumasoft.pl/wp-content/uploads/2021/05/patches_ckeditor_patch_file.png" alt="" class="wp-image-23" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/05/patches_ckeditor_patch_file.png 859w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/patches_ckeditor_patch_file-300x47.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/patches_ckeditor_patch_file-768x122.png 768w" sizes="auto, (max-width: 859px) 100vw, 859px" /></figure></div>



<p>What&#8217;s interesting, this file is basically a&nbsp;<em>git diff</em>&nbsp;between the original and modified&nbsp;<em>ckeditor.js</em>&nbsp;files:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="362" src="http://blog.yumasoft.pl/wp-content/uploads/2021/05/patch_file_contents_git_diff-1024x362.png" alt="" class="wp-image-24" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/05/patch_file_contents_git_diff-1024x362.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/patch_file_contents_git_diff-300x106.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/patch_file_contents_git_diff-768x271.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/patch_file_contents_git_diff.png 1237w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<p>When we now execute&nbsp;<strong>npx patch-package</strong>&nbsp;command, the changes we&#8217;ve made before are applied to the actual npm library&#8217;s files:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="419" height="82" src="http://blog.yumasoft.pl/wp-content/uploads/2021/05/patch-package_applying_patches.png" alt="" class="wp-image-25" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/05/patch-package_applying_patches.png 419w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/patch-package_applying_patches-300x59.png 300w" sizes="auto, (max-width: 419px) 100vw, 419px" /></figure></div>



<p>This is great, because we can now add this command&nbsp;<strong>patch-package</strong>&nbsp;to our build script, so our custom changes to the libraries are applied on every build. It&#8217;s very quick and works like a charm. That&#8217;s how we managed to easily replace npm library with our own implementation ?</p>



<p>We should check in the patch files to our GitHub repository, so our teammates have them locally and our build server can also apply the patches.</p>



<p>Since this moment, we modified the npm library&#8217;s code, so everyone who imports and uses this library in our app will be actually using our modified version of it.</p>



<p>If needed, we can easily revert the changes with&nbsp;<strong>npx patch-package &#8211;reverse</strong>. It restores the npm libraries to their original versions (thanks to the patch file stored as&nbsp;<em>git diff</em>).</p>



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



<p>I hope you found this article helpful. You can use the&nbsp;<em>patch-package</em>&nbsp;library not only to replace the npm library&#8217;s source files, but also as a quick-fix for bugs found in these libraries. No need to wait for the open-source community to fix the issue or the PR to be merged. You can make the patch quickly and at the same time report the issue on the concerned package&#8217;s GitHub. The tool even offers a feature to create a GH issue in the original repo with the diff containing your changes by using command&nbsp;<strong>npx patch-package package-name &#8211;create-issue</strong>.</p>



<p>For us at Yumasoft, the&nbsp;<a href="https://www.npmjs.com/package/patch-package" target="_blank" rel="noreferrer noopener">patch-package</a>&nbsp;is a life-saver. Maybe it also saves you some headache? ?</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/05/how-to-replace-npm-library-with-your-own-implementation/">How To Replace npm Library With Your Own Implementation?</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/05/how-to-replace-npm-library-with-your-own-implementation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Resources to learn JavaScript</title>
		<link>https://blog.yumasoft.pl/2020/04/resources-to-learn-javascript/</link>
					<comments>https://blog.yumasoft.pl/2020/04/resources-to-learn-javascript/#comments</comments>
		
		<dc:creator><![CDATA[Grzesiek]]></dc:creator>
		<pubDate>Mon, 20 Apr 2020 07:46:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[front end]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[web development]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=56</guid>

					<description><![CDATA[<p>When starting your journey with JavaScript, you should have knowledge about valuable information sources. In my opinion, having such lore can save you a lot of time and it can greatly accelerate your learning process. In this article I will try to present you some web pages with JavaScript resources, from widely renowned to slightly&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2020/04/resources-to-learn-javascript/">Resources to learn JavaScript</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>When starting your journey with JavaScript, you should have knowledge about valuable information sources. In my opinion, having such lore can save you a lot of time and it can greatly accelerate your learning process. In this article I will try to present you some web pages with JavaScript resources, from widely renowned to slightly underrated ones. I hope that it will help you to increase your skill without pointless wandering through internet depths.</p>



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



<h3 class="wp-block-heading" id="mdnwebdocs">MDN web docs</h3>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" src="https://strapi.yumasoft.pl/uploads/d801a4e496064859bcbed3c9fed16aa9.jpg" alt="text"/><figcaption><em><strong>Photo credit: developer.mozilla.org</strong></em></figcaption></figure></div>



<p>The most recognized JavaScript resource is MDN web docs (MDN stands for Mozilla Developer Network) provided and maintained by Mozilla Corporation. While other programming languages usually have their official documentation, JS doesn’t have one (It actually depends od how do you treat ECMAScript, because it has official documetation). And the reason behind it is that a few teams worked on developing JS. But nevertheless, we have MDN web docs website, which is almost treated like official documentation for the JavaScript community. Information provided by maintainers is truly comprehensive. In my developer career, I have never experienced lack of crucial information on subjects of interest. What is more, this website provides sufficient tutorials for web development including JavaScript, HTML and CSS, divided into different levels of expertise.</p>



<p>Links:<br>MDN:&nbsp;<a href="https://developer.mozilla.org/pl/">https://developer.mozilla.org/pl/</a><br>Tutorials:&nbsp;<a href="https://developer.mozilla.org/pl/docs/Web/Tutorials">https://developer.mozilla.org/pl/docs/Web/Tutorials</a></p>



<h3 class="wp-block-heading" id="ecmascriptdocs">ECMAScript docs</h3>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" src="https://strapi.yumasoft.pl/uploads/2b29ac5fbb324758be8cf01525f124e7.png" alt="text"/><figcaption><strong><em>Photo credit: ecma-international.org</em></strong></figcaption></figure></div>



<p>ECMAScript was created to simply standardize JavaScript, because maintaining multiple independent implementations would be unbelievably hard without some sort of a standard. In ECMAScript docs you can find what JavaScript should look like and in almost every case it does. For example the latest official release of ECMAScript is named ES2020. Ecma international also provides information about future additions to the standard under the name ESNext. Technically the ECMAscript docs are the closest you can get to the official JavaScript docs. I personally wouldn’t recommend reading them for beginners, because complicated language and covered subjects might seem a little underwhelming.</p>



<p>Links:<br>Newest ECMAScript Docs:&nbsp;<a href="https://tc39.es/ecma262/#sec-fundamental-objects">https://tc39.es/ecma262/#sec-fundamental-objects</a></p>



<h3 class="wp-block-heading" id="javascriptinfo">Javascript.info</h3>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" src="https://strapi.yumasoft.pl/uploads/3802aea22e0047cf82cb88209785abe7.png" alt="text"/><figcaption><em><strong>Photo credit: javascript.info</strong></em></figcaption></figure></div>



<p>Javascript.info is a great and clearly made tutorial website. The tutorial itself covers JavaScript as a programming language and matter of working with a browser. The amount of information crucial to gain essential knowledge is perfect. I would say, if you want to maximize your learning speed, javascript.info is a right choice. In comparison to MDN docs, it is easier to get essentials and don’t get overloaded with information. Additionally, almost every topic has some bonus, usually it is some trick that is hard to invent on your own or explanation of unusual JavaScript behaviour. In my honest opinion, this site is the best way to start learning JavaScript &#8211; In my case it was a turning point.</p>



<p>Links:<br>Javascript.info:&nbsp;<a href="https://javascript.info/">https://javascript.info/</a></p>



<h3 class="wp-block-heading" id="codecademy">Codecademy</h3>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" src="https://strapi.yumasoft.pl/uploads/5df2756104cd4b22a8b87e1bb63c9884.png" alt="text"/><figcaption><em><strong>Photo credit: codecademy.com</strong></em></figcaption></figure></div>



<p>Codecademy is an interactive learning site. You can take courses on a large amount of languages, but we are interested in JavaScript. If you are starting from scratch it is a way to go. Easy and well explained tasks will softly inaugurate you. The biggest advantage of this site is that you don’t need any code environment, you just write your code inside a special container right on the website. At the beginning, you already have some code written and you just have to change something or insert some variable or value, which sets the entry threshold really low. The downside of codecademy is “PRO” content, if you want to have access to everything, you must upgrade your account to PRO status and the lowest-priced upgrade costs 19.99$ a month.</p>



<p>Links:<br>Codecademy:&nbsp;<a href="https://www.codecademy.com/">https://www.codecademy.com/</a></p>



<h3 class="wp-block-heading" id="udemy">Udemy</h3>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" src="https://strapi.yumasoft.pl/uploads/722779c8f6d640b78599d74b0a72bef2.png" alt="text"/><figcaption><em><strong>Photo credit: udemy.com</strong></em></figcaption></figure></div>



<p>Video based tutorials have gained huge popularity in recent years. This form of studying is known as the easiest and really effective, so the huge increase in popularity is understandable. Udemy is a great online learning platform with a wide range of courses. It doesn&#8217;t only have videos, online tests, zip packages with source code and community conversations are a huge part of Udemy success. There are some free courses, but the better ones are paid. The prices are really high, but you can encounter 90% discounts almost every two weeks.</p>



<p>Links:<br>Udemy:&nbsp;<a href="https://www.udemy.com/">https://www.udemy.com/</a></p>



<p>Some valuable udemy courses:<br><a href="https://yumasoft.pl/blogpost/[https://www.udemy.com/course/modern-javascript/">https://www.udemy.com/course/modern-javascript/</a><br><a href="https://www.udemy.com/course/modern-javascript-from-the-beginning/">https://www.udemy.com/course/modern-javascript-from-the-beginning/</a><br><a href="https://www.udemy.com/course/js-algorithms-and-data-structures-masterclass/">https://www.udemy.com/course/js-algorithms-and-data-structures-masterclass/</a><br><a href="https://www.udemy.com/course/javascript-beginners-complete-tutorial/">https://www.udemy.com/course/javascript-beginners-complete-tutorial/</a></p>



<h3 class="wp-block-heading" id="codewars">Codewars</h3>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" src="https://strapi.yumasoft.pl/uploads/b52c50ef101f4fd3afd05f995de1749b.png" alt="text"/><figcaption><em><strong>Photo credit: codewars.com</strong></em></figcaption></figure></div>



<p>Learning by doing is pretty good, but if you ask me, learning by doing and competing is just amazing! Codewars is an online learning site, where you can complete challenges, get points for it and compare your solution to others. The challenges are divided into different levels of expertise and your account has a level itself to give you proper tasks. You can get better by completing challenges, but the amount of knowledge gained through studying solutions made by more advanced people is huge. Some code pieces you will find there will change your way of thinking and present you unthinkable actions. You can’t miss codewars!</p>



<p>Links:<br>Codewars:&nbsp;<a href="https://www.codewars.com/">https://www.codewars.com/</a></p>



<h3 class="wp-block-heading" id="w3schools">W3Schools</h3>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" src="https://strapi.yumasoft.pl/uploads/5bda54f36b624b39aeec0dc07cb56deb.jpg" alt="text"/><figcaption><em><strong>Photo credit: w3schools.com</strong></em></figcaption></figure></div>



<p>W3Schools is an old-school tutorial page. You can start your web development path using this webpage, but you will run out of resources quickly. Topics are explained quite well for beginners, but explanations are simple and minimalistic. Lack of crucial topics are enough to say W3Schools is insufficient for more advanced people. Nevertheless, this webpage is worth checking.</p>



<p>Links:<br>W3Schools:&nbsp;<a href="https://www.w3schools.com/">https://www.w3schools.com/</a><br>JS tutorial:&nbsp;<a href="https://www.w3schools.com/js/default.asp">https://www.w3schools.com/js/default.asp</a></p>



<h3 class="wp-block-heading" id="youtubechannels">Youtube channels</h3>



<p>It is an unlimited source of knowledge. You can find a huge amount of tutorials, explanations and examples on youtube. Some youtubers even upload some of their content from Udemy, so it is good to pay attention to several channels. I personally really enjoy crash courses on youtube, you can choose some technology and get essentials in under two hours.</p>



<p>Good channels:</p>



<p>Fun Fun Function:&nbsp;<a href="https://www.youtube.com/channel/UCO1cgjhGzsSYb1rsB4bFe4Q">https://www.youtube.com/channel/UCO1cgjhGzsSYb1rsB4bFe4Q</a><br>Traversy Media:&nbsp;<a href="https://www.youtube.com/user/TechGuyWeb">https://www.youtube.com/user/TechGuyWeb</a><br>freeCdeCamp.org:&nbsp;<a href="https://www.youtube.com/channel/UC8butISFwT-Wl7EV0hUK0BQ">https://www.youtube.com/channel/UC8butISFwT-Wl7EV0hUK0BQ</a><br>Udemy Tech:&nbsp;<a href="https://yumasoft.pl/blogpost/[https://www.youtube.com/channel/UCU6e4MJtvlcX5DBLP1cq8hQ">https://www.youtube.com/channel/UCU6e4MJtvlcX5DBLP1cq8hQ</a><br>Academind:&nbsp;<a href="https://www.youtube.com/channel/UCSJbGtTlrDami-tDGPUV9-w">https://www.youtube.com/channel/UCSJbGtTlrDami-tDGPUV9-w</a><br>Dev Ed:&nbsp;<a href="https://www.youtube.com/channel/UClb90NQQcskPUGDIXsQEz5Q">https://www.youtube.com/channel/UClb90NQQcskPUGDIXsQEz5Q</a><br>The Net Ninja:&nbsp;<a href="https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg">https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg</a><br>Techsith:&nbsp;<a href="https://www.youtube.com/user/techSithTube">https://www.youtube.com/user/techSithTube</a></p>



<h3 class="wp-block-heading" id="summary">Summary</h3>



<p>There are a lot of javascript resources. They may vary within the quality, but all of them are worth trying. In my opinion, dividing your time into different resources gives the best results in the studying process, because you avoid monotony. I will try to pick the best choices in some categories and write them below.</p>



<p><strong>Most comprehensive docs and tutorials:</strong>&nbsp;MDN web docs / ECMAScript docs<br><strong>Most efficient tutorial:</strong>&nbsp;Javascript.info<br><strong>Best for beginners:</strong>&nbsp;Codecademy</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2020/04/resources-to-learn-javascript/">Resources to learn JavaScript</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2020/04/resources-to-learn-javascript/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
