<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tyler Meyer</title>
    <description>The latest articles on DEV Community by Tyler Meyer (@tymey).</description>
    <link>https://dev.to/tymey</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2116198%2F670e3ccf-0ac3-444d-8f5e-a51ef711e1dc.jpeg</url>
      <title>DEV Community: Tyler Meyer</title>
      <link>https://dev.to/tymey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tymey"/>
    <language>en</language>
    <item>
      <title>Jest: A Delightful JavaScript Testing Framework</title>
      <dc:creator>Tyler Meyer</dc:creator>
      <pubDate>Mon, 10 Feb 2025 08:41:25 +0000</pubDate>
      <link>https://dev.to/tymey/jest-a-delightful-javascript-testing-framework-1je9</link>
      <guid>https://dev.to/tymey/jest-a-delightful-javascript-testing-framework-1je9</guid>
      <description>&lt;h1&gt;
  
  
  What is Jest?
&lt;/h1&gt;

&lt;p&gt;To quote the Jest Core Team, "Jest is a JavaScript testing framework designed to ensure correctness of any JavaScript codebase." &lt;a href="https://jestjs.io/" rel="noopener noreferrer"&gt;[1]&lt;/a&gt; Regarding software development, testing code provides a safety net that can prevent bugs and improve the code written. Setting up team workflows using Test Driven Development and Continuous Integration requires a reliable testing framework. Throughout this article, I will go over the strengths of Jest, the downsides, tips for setting up a test suite, and how to write tests.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Strengths of Jest
&lt;/h1&gt;

&lt;p&gt;I will compare Jest to another testing framework called Mocha for these strengths. Depending on the use case, developers will find either of these testing frameworks practical, depending on their needs. While critiquing Mocha, I will also point out its strength compared to Jest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simplicity
&lt;/h2&gt;

&lt;p&gt;Jest is easier to set up than Mocha. Mocha usually requires additional external tools to create a complete testing suite, while Jest works independently without additional libraries or tools. &lt;a href="https://saucelabs.com/resources/blog/jest-vs-mocha" rel="noopener noreferrer"&gt;[2]&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Snapshot Testing
&lt;/h2&gt;

&lt;p&gt;Snapshot testing is a Jest tool that ensures the UI does not change unexpectedly. Snapshot testing works by rendering a UI component, taking a snapshot, and then comparing the snapshot to a reference of the expected snapshot alongside the test. &lt;a href="https://jestjs.io/docs/snapshot-testing" rel="noopener noreferrer"&gt;[3]&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  React &amp;amp; TypeScript Integration
&lt;/h2&gt;

&lt;p&gt;Meta initially developed Jest to address their internal testing needs for their chat feature in 2011, which means it was primarily designed to work with React applications. Jest also provides support for TypeScript projects that want to ensure type safety.&lt;/p&gt;

&lt;h2&gt;
  
  
  Community Support
&lt;/h2&gt;

&lt;p&gt;Jest provides extensive documentation, and with a large community, you will find help with troubleshooting issues and best practices to follow. &lt;/p&gt;

&lt;h1&gt;
  
  
  The Potential Downsides of Jest
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Not as Flexible
&lt;/h2&gt;

&lt;p&gt;Compared to frameworks like Mocha, Jest is less flexible for customized testing scenarios. Since Mocha is designed to integrate with external tools, you can write tests for your specific needs. Creating custom test suites with Jest will require more work, which may cause slower performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance Concerns
&lt;/h2&gt;

&lt;p&gt;While Jest is generally fast, extensive test suites might sometimes experience performance degradation. To counter this, you can break more extensive tests into smaller, more focused units. &lt;/p&gt;

&lt;h1&gt;
  
  
  Tips for Setting Up Jest Test Suites
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Configure Jest
&lt;/h2&gt;

&lt;p&gt;Jest provides an extensive list of &lt;a href="https://jestjs.io/docs/configuration" rel="noopener noreferrer"&gt;options&lt;/a&gt; for configuring the testing framework for your needs, depending on the type of testing you're conducting. I'll provide you with some sample code for testing server endpoints.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// jest.config.js&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;preset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ts-jest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;globalSetup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./setup.ts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;globalTeardown&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./teardown.ts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I prefer to use TypeScript to provide type safety for my code. By installing &lt;code&gt;ts-jest&lt;/code&gt; and setting up a preset for it, Jest will recognize when I'm using TypeScript and transpile my code for testing. &lt;code&gt;globalSetup&lt;/code&gt; and &lt;code&gt;globalTeardown&lt;/code&gt; provide a way to perform actions before and after all test suites have run. In the following two code snippets, I'll set up a test server and then close the server in the teardown.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// setup.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Application&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;apiRouter&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../../server/api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  

&lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="nb"&gt;global&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;testServer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;promiseListen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;exApp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Application&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Server&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Server&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;exApp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server listening on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;

      &lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;apiRouter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Beginning of endpoints to be tested&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="na"&gt;testServer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;promiseListen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;globalThis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;testServer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;testServer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;globalThis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Failed to start server before testing:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// teardown.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="nb"&gt;global&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;testServer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;promiseServerClose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;promiseServerClose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;globalThis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;testServer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Failed to close server after testing:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These two code snippets run before and after all test suites. I use the globalThis object to pass the server created in &lt;code&gt;setup.ts&lt;/code&gt; to &lt;code&gt;teardown.ts&lt;/code&gt; to close the server once our tests are finished. I also pass the PORT number to globalThis to use in my test suites when they request the test server.&lt;/p&gt;

&lt;p&gt;If you need a different configuration for another set of tests, you can move your previously created config, setup, and teardown into its own folder along with the test files. In the root directory, you can use one jest config to list the projects to be tested with the &lt;code&gt;jest&lt;/code&gt; command as your test script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// jest.config.js&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;projects&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tests/server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// The new location of the previous code snippets&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Jest will go to each project and run the tests using the correct configuration.&lt;/p&gt;

&lt;h1&gt;
  
  
  Writing Tests With Jest
&lt;/h1&gt;

&lt;p&gt;I will briefly go over two code snippets to show you how to write a test using the Jest testing framework. The first example will be testing for a function. The second example will make use of the test server we created earlier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// myFunc.ts&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// myFunc.test.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;myFunc&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./myFunc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Testing myFunc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myFunc returns something&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toBeDefined&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Something is returned&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myFunc returns an Array&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Must be true&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myFunc creates an array of correct length&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toHaveLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Array's length is three&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myFunc creates the correct array&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toEqual&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="c1"&gt;// Recursively check all items in the returned array for equality&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The outer function &lt;code&gt;describe&lt;/code&gt; defines the current test suite. For the test suite to pass, it needs at least one &lt;code&gt;test&lt;/code&gt;, and every test must pass. The inner functions &lt;code&gt;test&lt;/code&gt; are titled to provide information to the developer when a test doesn't pass. There are several ways to test using &lt;code&gt;expect&lt;/code&gt;, and it comes with many &lt;a href="https://jestjs.io/docs/expect" rel="noopener noreferrer"&gt;"matchers"&lt;/a&gt; that let you validate your code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// tests/server/api.test.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="nb"&gt;global&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;testServer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;globalThis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// To keep the port consistent across tests&lt;/span&gt;

&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Testing /api Endpoints&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;test &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET /api/users responds with 200 status code&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`http://localhost:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/api/users`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;test &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET /api/users responds with users data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`http://localhost:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/api/user`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBeDefined&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Something is sent back&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// An array is sent back&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;toBeTruthy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// The array contains a user&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;toHaveProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;username&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// User has 'username'&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nx"&gt;not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toHaveProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUPER_SECRET_PASSWORD&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// Does not send back their 'SUPER_SECRET_PASSWORD'&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using async/await, we can make asynchronous calls and test the response we receive. In the second test, I used several matchers for one test, which is allowed if you want to check several aspects of one action. I also used the &lt;code&gt;not&lt;/code&gt; key for the matchers, which works as you'd expect by negating the expectation.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;The technology used in an application is a major consideration in software development. There isn't one perfect testing framework, but Jest is great if you're new to test-driven development and Continuous Integration. With its simplicity, large community, and relatively fast execution, you will be pleased with what you can accomplish with Jest.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;br&gt;
Tyler Meyer&lt;/p&gt;

&lt;h1&gt;
  
  
  Sources
&lt;/h1&gt;

&lt;p&gt;[1] &lt;a href="https://jestjs.io/" rel="noopener noreferrer"&gt;https://jestjs.io/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[2] &lt;a href="https://saucelabs.com/resources/blog/jest-vs-mocha" rel="noopener noreferrer"&gt;https://saucelabs.com/resources/blog/jest-vs-mocha&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[3] &lt;a href="https://jestjs.io/docs/snapshot-testing" rel="noopener noreferrer"&gt;https://jestjs.io/docs/snapshot-testing&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[4] &lt;a href="https://apidog.com/blog/jest-vs-jasmine/" rel="noopener noreferrer"&gt;https://apidog.com/blog/jest-vs-jasmine/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[5] &lt;a href="https://jestjs.io/docs/configuration" rel="noopener noreferrer"&gt;https://jestjs.io/docs/configuration&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[6] &lt;a href="https://jestjs.io/docs/expect" rel="noopener noreferrer"&gt;https://jestjs.io/docs/expect&lt;/a&gt;&lt;/p&gt;

</description>
      <category>jest</category>
      <category>testing</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>TypeScript: Learning the Basics + React</title>
      <dc:creator>Tyler Meyer</dc:creator>
      <pubDate>Mon, 27 Jan 2025 08:09:50 +0000</pubDate>
      <link>https://dev.to/tymey/typescript-learning-the-basics-1h49</link>
      <guid>https://dev.to/tymey/typescript-learning-the-basics-1h49</guid>
      <description>&lt;p&gt;In this article, we'll discuss why developers use TypeScript and the basics of its usage. Before we get started, I recently wrote about TypeScript in a previous article. In that article, I explained what it is and how to set up your environment to code with it. If you need a refresher, you can find that article &lt;a href="https://dev.to/tymey/typescript-getting-started-4plk"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why Use TypeScript?
&lt;/h1&gt;

&lt;p&gt;When JavaScript was first written, its biggest appeal was its flexibility. The coding language included type conversion for specific operations, allowing developers to create algorithms that could take different data types as inputs and return possibly different outputs. For some, this freedom sounds great, but any experienced developer will point out that this lack of type safety may be its downside. Without type safety, codebases become harder to maintain, and applications become more challenging to scale. Let's quickly discuss a few positives of TypeScript:&lt;/p&gt;

&lt;h2&gt;
  
  
  Catch Bugs Before They Happen
&lt;/h2&gt;

&lt;p&gt;By requiring developers to use static types for variables and functions, they can ensure that the data they work with is what they expect. When expecting certain types of data, you can create a more reliable algorithm. Once a static type is set, TypeScript can warn you about possible bugs before they even happen. Even the TypeScript compiler will prevent code from compiling if there is potentially a bug. With just a little more code, the code base becomes more reliable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scalability &amp;amp; Maintainability
&lt;/h2&gt;

&lt;p&gt;As a codebase grows with additional features or functions, the question of efficiency arises. Will my code be used properly in other modules? How can I be sure that it will work properly? TypeScript. The ability to create types and interfaces helps you ensure that your code is being appropriately used outside of its module. In React applications, you'll pass props from component to component, and some components will be used in different contexts throughout the application. Setting up type safety with components means you can ensure that a component will work properly because you know the data the component should receive. We'll explore these ideas later. Simply put, you can have peace of mind that you will not have to revisit your code multiple times to ensure it works in multiple contexts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Long Live Your Code
&lt;/h2&gt;

&lt;p&gt;The goal of writing code is for it to be readable by others, not just for it to work. As time passes, other developers will work with your code to scale an application further or perhaps debug an issue. These developers will be entering an unknown codebase, and TypeScript helps them understand the intention of your code and gets them working faster. They'll know the data types you were expecting to input and output. While this may sound simple, the convenience cannot understated. A developer's workflow will improve immediately.&lt;/p&gt;

&lt;h1&gt;
  
  
  Explicit Typing
&lt;/h1&gt;

&lt;p&gt;The strength of TypeScript is the ability to type your variables explicitly. TypeScript can figure out the type for a variable implicitly, but this leaves the door open for unexpected outcomes if a variable could implicitly have two types, but you only planned for one. Let's take a look at a few examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tyler Meyer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Type 'number' is not assignable to type 'string'.&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Will not run with the Type Error on line 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Line 1&lt;/strong&gt;: &lt;em&gt;We explicitly state that author has type 'string'.&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;Line 2&lt;/strong&gt;: &lt;em&gt;We attempt to reassign author to 32, but we are blocked by TypeScript since 32 is a 'number'.&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;Line 3&lt;/strong&gt;: &lt;em&gt;This will not be logged until we address the issue on Line 2.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;studentGrades&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;93&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;studentGrades&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;studentGrades&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Error&lt;/span&gt;
&lt;span class="nx"&gt;studentGrades&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;97&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Line 1&lt;/strong&gt;: &lt;em&gt;We initialize an array with some grades for a student, and we explicitly state the array should only contain numbers.&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;Line 2&lt;/strong&gt;: &lt;em&gt;This adheres to the type inside the array, so it gets pushed in.&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;Line 3&lt;/strong&gt;: &lt;em&gt;An error is thrown because the argument "A" is a string, which is not assignable to the parameter of type "number."&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;Line 4&lt;/strong&gt;: *Even though "97" is a number, the data type is a string so it gets rejected.&lt;/p&gt;
&lt;h1&gt;
  
  
  Aliases &amp;amp; Interfaces
&lt;/h1&gt;

&lt;p&gt;As our codebase grows and we use more complex data types, the type (alias) and interface keywords become very helpful. Aliases can be used on primitive data types as well, but for my example, I'll be using the object data type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lovesCoding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;coolAuthor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tyler&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Meyer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lovesCoding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of using a generic &lt;code&gt;object&lt;/code&gt; type, &lt;code&gt;coolAuthor&lt;/code&gt; is assigned the &lt;code&gt;Author&lt;/code&gt; type. An alias just needs to be defined before being used. Keeping your types in a "types" directory for your project will enable our team members to using these types in their modules and keep your team on the same page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Book&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
  &lt;span class="nx"&gt;numberOfPages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Textbook&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Book&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;calculusBook&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Textbook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Calculus 4 Dummies&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;numberOfPages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;58&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Calculus&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Interfaces can only be used on object types. This is because of their ability to extend other interfaces, much like classes can be used to extend other classes. Since &lt;code&gt;Textbook&lt;/code&gt; extends &lt;code&gt;Book&lt;/code&gt;, it inherits the typing for &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;numberOfPages&lt;/code&gt; along with the typing for &lt;code&gt;subject&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  React With Typescript
&lt;/h1&gt;

&lt;p&gt;Assuming your project is set up to transpile .tsx files (React files using TypeScript and JSX) into readable JavaScript for the browser, let's explore how you can use TypeScript to manage the flow of data through your components.&lt;/p&gt;

&lt;h2&gt;
  
  
  General Functions
&lt;/h2&gt;

&lt;p&gt;Before I show an example of React code, let's take a look at how TypeScript is used to help validate functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`My name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, and I am &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old.`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tyler&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// Will successfully run.&lt;/span&gt;
&lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;profession&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Photographer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// Error&lt;/span&gt;
&lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sadie&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// Error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By creating an alias beforehand, we can destructure our input object parameter for the keys we want to use in the &lt;code&gt;greeting&lt;/code&gt; function. The first function call follows the explicit types set by the alias and will run without any typing errors. The second function call doesn't use the &lt;code&gt;age&lt;/code&gt; property and will throw a typing error. The third function call using a 'string' type instead of a 'number' type for the &lt;code&gt;age&lt;/code&gt; property and will throw a typing error.&lt;/p&gt;

&lt;h2&gt;
  
  
  React Function Components
&lt;/h2&gt;

&lt;p&gt;Components in React are made by declaring a function and returning JSX (HTML-like code) to render on the page. We pass props from a parent node to a child node to create dynamic content. These props from the parent component are placed into the parameters of the child component as an object with keys named after the props. Since we'll know the props, it makes sense that we'll want to destructure them in the child component. Now it's time to set up type safety with TypeScript. Make sure you use the .tsx extension on your component files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;This is my child:&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Child&lt;/span&gt; 
        &lt;span class="na"&gt;person&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tyler&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;profession&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Software Developer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; 
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ChildProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;person&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;profession&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;ChildProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`Name: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`Age: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`Profession: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;profession&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;When transitioning from JavaScript to TypeScript, you're inheriting a new coding language that puts up guardrails to help the developer write reliable code with fewer bugs as a project scales. While you may initially be writing more code, you'll save time in the future because fewer bugs will be encountered and fewer refactors to account for the type conversion from JavaScript.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;br&gt;
Tyler Meyer&lt;/p&gt;

&lt;h3&gt;
  
  
  Sources:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://medium.com/@ZaradarTR/why-i-typescript-aa891184279b" rel="noopener noreferrer"&gt;https://medium.com/@ZaradarTR/why-i-typescript-aa891184279b&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/typescript/typescript_simple_types.php" rel="noopener noreferrer"&gt;https://www.w3schools.com/typescript/typescript_simple_types.php&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/typescript/typescript_arrays.php" rel="noopener noreferrer"&gt;https://www.w3schools.com/typescript/typescript_arrays.php&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/typescript/typescript_aliases_and_interfaces.php" rel="noopener noreferrer"&gt;https://www.w3schools.com/typescript/typescript_aliases_and_interfaces.php&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/typescript/typescript_functions.php" rel="noopener noreferrer"&gt;https://www.w3schools.com/typescript/typescript_functions.php&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>learning</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>TypeScript: Getting Started With TSConfig Options</title>
      <dc:creator>Tyler Meyer</dc:creator>
      <pubDate>Mon, 13 Jan 2025 05:29:41 +0000</pubDate>
      <link>https://dev.to/tymey/typescript-getting-started-4plk</link>
      <guid>https://dev.to/tymey/typescript-getting-started-4plk</guid>
      <description>&lt;h2&gt;
  
  
  What is TypeScript?
&lt;/h2&gt;

&lt;p&gt;TypeScript is a superset of JavaScript. This means you can use JavaScript syntax in TypeScript, so learning JavaScript before jumping into TypeScript is best. With that disclaimer, let’s talk about what TypeScript is all about and how we can set up a project for TypeScript. &lt;/p&gt;

&lt;p&gt;According to the documentation for TypeScript, “TypeScript is a strongly typed programming language that builds on JavaScript.”&lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;[1]&lt;/a&gt; The TypeScript language adds type syntax to help you catch errors early during development and keeps safeguards up while your development team grows as a project scales. TypeScript allows you to define the datatypes for variables and interfaces for objects. TypeScript uses these definitions to check your code for errors as it compiles and informs you when you are not following the definitions you set beforehand.&lt;/p&gt;

&lt;p&gt;If you're thinking, "Why do I need to write more code just to use TypeScript when I can be more careful in the first place?" You're not wrong, but humans can make errors, and we often do when working for long periods. If this same thinking were applied to construction, then the additional scaffolding and safety procedures would require more work and time, and we can save time by just being careful at the work site.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj11ya22ywmyri4m2nbhy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj11ya22ywmyri4m2nbhy.gif" alt="Roofer tries to carry roofing materials up a ladder by themselves without scaffolding put up or safety equipment on, and they drop the roofing materials, knock the ladder over, and fall off the roof with the roofing material landing on top of them. The roofer does get up at the end of the GIF." width="220" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See? In an effort to save time, this roofer may have wasted more time and resources. This is just one worker. Imagine a team of workers working on a project as more team members are added. This is the reality of software development; having a plan to catch these mistakes will help you and your team in the long run.&lt;/p&gt;

&lt;h2&gt;
  
  
  TypeScript Examples
&lt;/h2&gt;

&lt;p&gt;The following examples come from the TypeScript documentation&lt;a href="https://www.typescriptlang.org/docs/handbook/2/basic-types.html" rel="noopener noreferrer"&gt;[2]&lt;/a&gt;, but the commentary is mine.&lt;/p&gt;

&lt;p&gt;TypeScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, today is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Brendan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TypeScript will catch the error when &lt;code&gt;greet&lt;/code&gt; is called. We defined &lt;code&gt;greet&lt;/code&gt; to receive two arguments, &lt;code&gt;person&lt;/code&gt; and &lt;code&gt;date&lt;/code&gt;, and we only supplied &lt;code&gt;person&lt;/code&gt;. TypeScript will catch this error when it compiles the code and lets you know it expected a second argument. In a way, TypeScript can be considered a linter for your code to catch these errors while you're working, but we can leverage the type syntax to help us further.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, today is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toDateString&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Maddison&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we add a type to the two arguments, &lt;code&gt;person&lt;/code&gt; must be a string, and &lt;code&gt;date&lt;/code&gt; must be a Date object for the &lt;code&gt;toDateString&lt;/code&gt; method. When &lt;code&gt;greet&lt;/code&gt; is called without the keyword &lt;code&gt;new&lt;/code&gt; before the second argument, &lt;code&gt;Date()&lt;/code&gt;, TypeScript will let you know it received a string instead of a Date for the &lt;code&gt;date&lt;/code&gt; parameter. Now, you can fix your error before developing further and having to trace back to this error when you receive an unexpected output while testing.&lt;/p&gt;

&lt;p&gt;Now that you've seen TypeScript in action let's examine the next steps in setting up your project to use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring TypeScript's Compiler: &lt;code&gt;tsconfig.json&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The default TypeScript compiling may not be what you are looking for, and there is a way to customize TypeScript for your needs, similar to using a linter, but it can do so much more.&lt;/p&gt;

&lt;p&gt;The first step is to create a &lt;code&gt;tsconfig.json&lt;/code&gt; file in your project's root directory. This file tells TypeScript which files should be included in the compilation process. In the &lt;code&gt;tsconfig.json&lt;/code&gt;, you can specify the directories from the root that should be included if you want more specificity using your JSON file's "includes" key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"include"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"src/**/*"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's talk about &lt;code&gt;"compilerOptions"&lt;/code&gt;. Trust me when I say there are a ton of options to choose from. This is a good thing but also terrifying if this is your first time using TypeScript. I'm going to break down a few of the popular choices to help you get started:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;allowJs&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"allowJs"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This option allows JavaScript files to be imported inside your project instead of only TypeScript. Typically, TypeScript assumes all imports are TypeScript and would give an error for imported JavaScript files, but this option allows us to use those imports and can be helpful when working with TypeScript and JavaScript in the same repository.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;esModuleInterop&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"esModuleInterop"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A namespace import in ES6 can only be an object, but since this is the same as using &lt;code&gt;require&lt;/code&gt; without &lt;code&gt;.default&lt;/code&gt;, we have allowed TypeScript to treat an object as a function. Instead of having to be careful about our imports, this option will fix this issue when TypeScript transpiles the code into JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;target&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"es6"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This option changes which JS features are downgraded and which are left intact when TypeScript transpiles your code into JavaScript. &lt;code&gt;es6&lt;/code&gt; is a good choice since most modern browsers support ES6, but you can designate any version of ECMAScript for this option to suit your needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;strict&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"strict"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This flag enables many different type checking behaviors. This will result in a stronger codebase with fewer errors. If you like to exclude certain type checking behaviors, check the documentation &lt;a href="https://www.typescriptlang.org/tsconfig/" rel="noopener noreferrer"&gt;[3]&lt;/a&gt; and set their options to false. If you only want a couple type checking behaviors, I would consider turn those on instead of using &lt;code&gt;strict&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;outDir&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"outDir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./dist"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When TypeScript transpiles your code into usable JavaScript, this option will emit those files into this directory.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;noEmit&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"noEmit"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This option stops all transpiled JavaScript files from emitting. This may sound silly since I just told you about &lt;code&gt;outDir&lt;/code&gt;, but TypeScript will always emit files and &lt;code&gt;outDir&lt;/code&gt; will direct the files to the correct location. &lt;code&gt;noEmit&lt;/code&gt; comes into play when you are already using another tool like Babel or Webpack to transpile and bundle your code. Emitting in this case would mean creating useless JavaScript files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;There you have it. Those are seven options to help you set up your configuration for TypeScript and how TypeScript can help you create a more stable code base. I recommend giving Matt Pocock's &lt;a href="https://www.totaltypescript.com/tsconfig-cheat-sheet" rel="noopener noreferrer"&gt;"The TSConfig Cheat Sheet"&lt;/a&gt; a read for more popular options to put in your &lt;code&gt;tsconfig.json&lt;/code&gt; and always refer to the TypeScript &lt;a href="https://www.typescriptlang.org/tsconfig/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; on the TSConfig before implementing any of these options.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;br&gt;
Tyler Meyer&lt;/p&gt;

&lt;h3&gt;
  
  
  Sources:
&lt;/h3&gt;

&lt;p&gt;[1] &lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;https://www.typescriptlang.org/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[2] &lt;a href="https://www.typescriptlang.org/docs/handbook/2/basic-types.html" rel="noopener noreferrer"&gt;https://www.typescriptlang.org/docs/handbook/2/basic-types.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[3] &lt;a href="https://www.typescriptlang.org/tsconfig/" rel="noopener noreferrer"&gt;https://www.typescriptlang.org/tsconfig/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[4] &lt;a href="https://www.totaltypescript.com/tsconfig-cheat-sheet" rel="noopener noreferrer"&gt;https://www.totaltypescript.com/tsconfig-cheat-sheet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>productivity</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Exploring the Use of C++ for Game Development</title>
      <dc:creator>Tyler Meyer</dc:creator>
      <pubDate>Mon, 02 Dec 2024 07:20:20 +0000</pubDate>
      <link>https://dev.to/tymey/why-are-people-choosing-c-for-game-development-29fp</link>
      <guid>https://dev.to/tymey/why-are-people-choosing-c-for-game-development-29fp</guid>
      <description>&lt;h2&gt;
  
  
  What to Consider When Developing a Video Game
&lt;/h2&gt;

&lt;p&gt;To make a video game of your own, you'll need a fun idea for a game, which is a big hurdle to overcome to start making a game. Let's assume you have an idea for a game and want to bring it to life. To do this, you'll need to write code to make everything work, art and sound design to make everything appealing, and you'll need to do anything you can to ensure your game runs smoothly and the experience is enjoyable for your user.&lt;/p&gt;

&lt;p&gt;The last piece of the puzzle of making the game run smoothly is the priority for many game developers, and choosing the correct language to code your game typically ties back to this issue. C++ is "renowned for its speed and flexibility, and [its] ability to communicate directly with hardware." &lt;a href="https://www.geeksforgeeks.org/cpp-for-game-development/" rel="noopener noreferrer"&gt;[1]&lt;/a&gt; Before jumping into the details of why C++ works so efficiently, let's explore a few other languages used for game development and where they may have an edge over C++.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparing C++ With Other Game Development Languages
&lt;/h2&gt;

&lt;p&gt;Before discussing C#(Sharp) and Java, I want to highlight a few other languages used in game development for different reasons. &lt;/p&gt;

&lt;h3&gt;
  
  
  JavaScript, Python, and Lua
&lt;/h3&gt;

&lt;p&gt;JavaScript can be found on web pages, servers, and even on Raspberry Pis, which has become a popular way to make portable game systems. A few game engines support JavaScript code, including "Impact.js, LÖVE, and Crafty.js." &lt;a href="https://www.orientsoftware.com/blog/gaming-programming-language/" rel="noopener noreferrer"&gt;[2]&lt;/a&gt; Python and Lua can be easier to pick up when learning a new programming language, and both come with support from many open-source game engines. These three languages have been mainly used for 2D games. They can also be used with 3D games, but it is recommended to use Python for 3D over the other two since JavaScript has limited support and Lua has no support. In a future article, I plan to spend more time researching these languages in the gaming space.&lt;/p&gt;

&lt;h3&gt;
  
  
  C#(Sharp)
&lt;/h3&gt;

&lt;p&gt;C# has support from many game engines, which include Unity and MonoGame. C# is similar to C++, and it is reported to be "less complicated and easier to set up with Visual Studio and VS Code as your Integrated Development Environment." &lt;a href="https://www.orientsoftware.com/blog/gaming-programming-language/" rel="noopener noreferrer"&gt;[2]&lt;/a&gt; There are many differences between C++ and C# as well, and they should be considered when deciding on the language you'll use. We're going to highlight a couple differences &lt;a href="https://www.geeksforgeeks.org/c-vs-c-sharp/" rel="noopener noreferrer"&gt;[3]&lt;/a&gt;:&lt;/p&gt;

&lt;h4&gt;
  
  
  Memory Management
&lt;/h4&gt;

&lt;p&gt;In C++, the programmer manually manages memory, while C# has an automatic garbage collector. C# is more convenient for this task, but C++ gives the programmer more control over how the memory is used, which can lead to optimizing code to run more quickly for smoother performance. Depending on the size of the project, this amount of control over memory may not be necessary if the project is small. &lt;/p&gt;

&lt;h4&gt;
  
  
  Pointers
&lt;/h4&gt;

&lt;p&gt;Pointers are a way of referencing an address in memory, and I will go into more detail later in the article. In C++, pointers can be used anywhere in the program, while C# can only use pointers in unsafe mode. "Unsafe code can create issues with stability and security due to its inherent complex syntax and potential for memory-related errors, such as stack overflow, accessing and overwriting system memory." &lt;a href="https://www.c-sharpcorner.com/UploadFile/f0b2ed/understanding-unsafe-code-in-C-Sharp/#:~:text=Definition%20of%20Unsafe%20Mode,to%20specify%20a%20block%20code." rel="noopener noreferrer"&gt;[4]&lt;/a&gt; This means that extra special care needs to used with the unsafe mode in C#, which means that pointers may need to be avoided when writing code for your project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Java
&lt;/h3&gt;

&lt;p&gt;Java is an object-oriented programming language similar to C++, and "the Java Virtual Machine (JVM) is used to run the bytecode, which makes it compatible with almost any platform." &lt;a href="https://www.orientsoftware.com/blog/gaming-programming-language/" rel="noopener noreferrer"&gt;[2]&lt;/a&gt; The differences between Java and C++ are similar to C# and let's explore why &lt;a href="https://www.geeksforgeeks.org/cpp-vs-java/" rel="noopener noreferrer"&gt;[5]&lt;/a&gt;:&lt;/p&gt;

&lt;h4&gt;
  
  
  Memory Management
&lt;/h4&gt;

&lt;p&gt;In Java, memory management is controlled by the Java Virtual Machine (JVM), while C++ is manually controlled.&lt;/p&gt;

&lt;h4&gt;
  
  
  Platform Dependency
&lt;/h4&gt;

&lt;p&gt;Both languages work on any platform. For Java, it uses the Java Virtual Machine (JVM) to accomplish this. For C++, it needs the correct compiler to compile the code for the correct platform.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pointers
&lt;/h4&gt;

&lt;p&gt;Java can use pointers, but there is limited support for them. C++ fully embraces pointers to the point that values can also be called by reference, and Java only calls by value for the pointers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing C++ For Video Game Development
&lt;/h2&gt;

&lt;p&gt;When developing a video game, resuing assets will happen naturally. Introducing obstacles for your player to overcome and returning to the same or similar obstacle later creates a sense of progression for your player to show them how they have grown throughout the experience or how they can look at an obstacle in different ways to overcome it. This structure sounds like a class in programming, a way to reuse code multiple times throughout an application. Picking an object-oriented programming language will be paramount for a smoother development period. Luckily, the languages I've mentioned all support class-based programming techniques. Let's look into why C++ is often chosen for game projects.&lt;/p&gt;

&lt;p&gt;Regarding performance, video games designed with C++ run more quickly and smoothly than other languages when creating games that can scale well. C++ accomplishes this by giving the programmer direct control over memory management. Other languages handle these tasks automatically with garbage collectors. "Understanding pointers, memory allocation, and memory leaks can help your game run smoothly without wasting valuable resources." &lt;a href="https://www.geeksforgeeks.org/cpp-for-game-development/" rel="noopener noreferrer"&gt;[1]&lt;/a&gt; We're going to break down these three things with a few coding examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pointers
&lt;/h3&gt;

&lt;p&gt;A pointer is a variable that stores the memory address as its value. Since a pointer stores an address, we can make a call-by-reference. Pointers can "create and manipulate dynamic data structures" &lt;a href="https://www.geeksforgeeks.org/cpp-pointers/" rel="noopener noreferrer"&gt;[6]&lt;/a&gt; and can be used to iterate over these data structures.&lt;/p&gt;

&lt;p&gt;To use a pointer, you need to define a pointer variable that matches the data type you will be referencing. The data type must be associated with a pointer so it "knows how many bytes the data is stored in." &lt;a href="https://www.geeksforgeeks.org/cpp-pointers/" rel="noopener noreferrer"&gt;[6]&lt;/a&gt; Use the unary operator &amp;amp; on the variable address you want to store on the previously created pointer.  To access the value stored at an address, use the unary operator * on the pointer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;bits/stdc++.h&amp;gt;
using namespace std;

void pointers() {
    int var = 20;

    // Declare pointer variable
    // Note that the data type of ptr and var must be the same
    int* ptr;

    // Declare pointer of a pointer variable
    int** ptr2;

    // Assign the address of a variable to a pointer
    ptr = &amp;amp;var;

    // Assign the address of a pointer to another pointer
    ptr2 = &amp;amp;ptr;


    // ptr holds the address of var
    cout &amp;lt;&amp;lt; "Value at ptr = " &amp;lt;&amp;lt; ptr &amp;lt;&amp;lt; endl;

    // var holds the value of 20
    cout &amp;lt;&amp;lt; "Value at var = " &amp;lt;&amp;lt; var &amp;lt;&amp;lt; endl;

    // * dereferences ptr to give the value of 20 
      // located at the address assigned to ptr
    cout &amp;lt;&amp;lt; "Value at *ptr = " &amp;lt;&amp;lt; *ptr &amp;lt;&amp;lt; endl;

    // ptr2 holds the address of ptr
    // Even pointers have addresses of their own
    cout &amp;lt;&amp;lt; "Value at ptr2 = " &amp;lt;&amp;lt; ptr2 &amp;lt;&amp;lt; endl;

    // Dereferencing ptr2 once reveals that 
      // ptr2 references the same address as ptr
    cout &amp;lt;&amp;lt; "Value at *ptr2 = " &amp;lt;&amp;lt; *ptr2 &amp;lt;&amp;lt; endl;

    // Dereferencing ptr2 twice reveals 20, 
      // the value you receive when dereferencing ptr once
    cout &amp;lt;&amp;lt; "Value at **ptr2 = " &amp;lt;&amp;lt; **ptr2 &amp;lt;&amp;lt; endl;
}

int main() {
    pointers();
    /*
     * Value at ptr = 0x6caebffc54
     * Value at var = 20
     * Value at *ptr = 20
     * Value at ptr2 = 0x6caebffc48
     * Value at *ptr2 = 0x6caebffc54
     * Value at **ptr2 = 20
     */
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we store the address to other data types, we simulate calling by reference. We can modify any data type within a function and reuse that updated data later in our code. &lt;/p&gt;

&lt;h3&gt;
  
  
  Dynamic Memory Allocation
&lt;/h3&gt;

&lt;p&gt;If we expect a certain maximum-sized input, we could prepare our program to have enough memory set aside to match the worst-case scenario. This could pose a problem as a project continues to scale larger and larger. What if we could pick how much memory we need only once we know how much is needed? This would prevent unneeded memory usage and allow our code to run more efficiently. This is the key principle behind dynamic memory allocation: only set aside enough memory space to accomplish the task at hand and then free up the space immediately after.&lt;/p&gt;

&lt;p&gt;Check out the following code &lt;a href="https://cplusplus.com/doc/tutorial/dynamic/" rel="noopener noreferrer"&gt;[7]&lt;/a&gt; with the addition of comments to help you see how we accomplish dynamic memory allocation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;new&amp;gt;
using namespace std;

int main() {
    // i =&amp;gt; Used for looping
    int i;
    // n =&amp;gt; Used for capturing the input for the first question
    int n;
    // ptr =&amp;gt; Pointer used to reference memory that is allocated
    int* ptr;

    // n is assigned the input from the user
    cout &amp;lt;&amp;lt; "How many numbers would you like to type? ";
    cin &amp;gt;&amp;gt; n;

    // new =&amp;gt; Allocates space match the input for n
    ptr = new (nothrow) int[n];

    // Check to make sure ptr points to a valid object
    if (ptr == nullptr) {
        cout &amp;lt;&amp;lt; "Memory allocation error!" &amp;lt;&amp;lt; endl;
    }

    else {
        for (i=0; i&amp;lt;n; i++) {
            // Ask for a number n times
            cout &amp;lt;&amp;lt; "Enter number: ";

            // Store the number entered in memory
            cin &amp;gt;&amp;gt; ptr[i];
        }

        // Reveal to the user the choices they made
        cout &amp;lt;&amp;lt; "You have entered: ";

        for (i=0; i&amp;lt;n; i++) {
            // Pull each number from the allocated memory
            cout &amp;lt;&amp;lt; ptr[i] &amp;lt;&amp;lt; ", ";
        }

        // After we finish with the task,
            // we free up the space taken using delete[]
        delete[] ptr;
    }
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcv1cw5m5yl8d8br2k7vr.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcv1cw5m5yl8d8br2k7vr.PNG" alt="The output shows a prompt to select how many numbers the user wants to type. The user picks 6. Then types 4, 8, 15, 16, 23, 42. Afterward, the program lists all the numbers typed before ending the program." width="433" height="283"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The use of the delete keyword is essential when coding in C++. Since programmers need to clear out their memory manually, developing good habits to clear out memory once it's finished being used will lead to less frustration and fewer bugs, and it avoids the dreaded Memory Leak.&lt;/p&gt;

&lt;h3&gt;
  
  
  Memory Leak
&lt;/h3&gt;

&lt;p&gt;In C++, there is no automatic garbage collection, which means that any memory that a programmer dynamically allocates throughout the lifetime of a program needs to be freed manually after its usage by the programmer once it is no longer needed. If a programmer forgets to free this memory after its usage, it will occupy the space while the program lives and will be unavailable to other processes. Often, a function may need to be called several times, and if each call allocates more memory without removing it, a lot of unused memory will take up space. This accumulation of unwanted memory usage is referred to as a memory leak. These can drastically slow down a program, but they are the avoidable price for faster running code. &lt;a href="https://www.geeksforgeeks.org/memory-leak-in-c-and-how-to-avoid-it/" rel="noopener noreferrer"&gt;[8]&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts on C++ For Game Development
&lt;/h2&gt;

&lt;p&gt;Having complete control over memory usage is a huge plus, or some might call it a "plus-plus." We create pointers to store the addresses of newly created data structures. After the stored memory has been used to completion, we remove the data from memory to free up space later in the program to prevent memory leaks. The object-oriented programming language aspect of C++ also shows the usefulness of reusing code.&lt;/p&gt;

&lt;p&gt;C++ isn't the only choice for a game development language. Java is portable and can be used across multiple platforms, but it is limited in making 3D games. C#(Sharp) is less complicated than C++, but it has little to no support for pointers unless you use unsafe mode, which is not recommended unless you take extreme care while programming in that mode.&lt;/p&gt;

&lt;p&gt;Due to the programmer's direct control of memory in C++, fast speeds can be achieved with pointers and dynamic memory allocation. Speed is an important consideration when designing a large-scale video game because an unresponsive or slow-to-respond gaming experience can leave your user unsatisfied.&lt;/p&gt;

&lt;p&gt;Happy Coding,&lt;br&gt;
Tyler Meyer&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;Sources: &lt;br&gt;
[1] &lt;a href="https://www.geeksforgeeks.org/cpp-for-game-development/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/cpp-for-game-development/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[2] &lt;a href="https://www.orientsoftware.com/blog/gaming-programming-language/" rel="noopener noreferrer"&gt;https://www.orientsoftware.com/blog/gaming-programming-language/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[3] &lt;a href="https://www.geeksforgeeks.org/c-vs-c-sharp/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/c-vs-c-sharp/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[4] &lt;a href="https://www.c-sharpcorner.com/UploadFile/f0b2ed/understanding-unsafe-code-in-C-Sharp/" rel="noopener noreferrer"&gt;https://www.c-sharpcorner.com/UploadFile/f0b2ed/understanding-unsafe-code-in-C-Sharp/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[5] &lt;a href="https://www.geeksforgeeks.org/cpp-vs-java/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/cpp-vs-java/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[6] &lt;a href="https://www.geeksforgeeks.org/cpp-pointers/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/cpp-pointers/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[7] &lt;a href="https://cplusplus.com/doc/tutorial/dynamic/" rel="noopener noreferrer"&gt;https://cplusplus.com/doc/tutorial/dynamic/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[8] &lt;a href="https://www.geeksforgeeks.org/memory-leak-in-c-and-how-to-avoid-it/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/memory-leak-in-c-and-how-to-avoid-it/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>cpp</category>
      <category>csharp</category>
      <category>java</category>
    </item>
    <item>
      <title>C++: An Object-Oriented Programming Language</title>
      <dc:creator>Tyler Meyer</dc:creator>
      <pubDate>Mon, 18 Nov 2024 05:39:25 +0000</pubDate>
      <link>https://dev.to/tymey/c-an-object-oriented-programming-language-4fog</link>
      <guid>https://dev.to/tymey/c-an-object-oriented-programming-language-4fog</guid>
      <description>&lt;h2&gt;
  
  
  What is C++?
&lt;/h2&gt;

&lt;p&gt;According to a definition written on TechTarget, "C++ is an object-oriented programming language and a superset of the C language." &lt;a href="https://www.techtarget.com/searchdatamanagement/definition/C" rel="noopener noreferrer"&gt;[1]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we call C++ a superset of the C language, this means that C++ can still run code from C and still produce the same result, but C++ comes with support for classes and objects. With the introduction of classes, C++ becomes the object-oriented programming language that is used to create large-scale applications as well as projects with constrained memory demands. A good place to start learning C++ is to learn about object-oriented programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object-Oriented Programming
&lt;/h2&gt;

&lt;p&gt;In a later example, I will show key features of the C++ language, but I will not be able to fully show you object-oriented programming, which I'll be shortening to OPP for the rest of this article.&lt;/p&gt;

&lt;p&gt;As the name suggests, this implementation revolves around the concept of objects. According to Wikipedia, "computer programs are designed by making them out of objects that interact with one another in OPP." &lt;a href="https://en.wikipedia.org/wiki/Object-oriented_programming" rel="noopener noreferrer"&gt;[2]&lt;/a&gt; This philosophy is great because objects can hold both data and methods and this information can be bundled together without having to be written with other code that it does not need to interact with. This is one of the benefits of OPP, and I am going to go over four key aspects of OPP, Inheritance, Abstraction, Encapsulation, and Polymorphism.&lt;/p&gt;

&lt;h4&gt;
  
  
  Inheritance
&lt;/h4&gt;

&lt;p&gt;With classes, we can create objects that share certain property names and methods, but at the time of creating the object, we can pass in values that make the instance of the object different from others with the same properties and methods. Each object inherits a particular set of properties and methods.&lt;/p&gt;

&lt;h4&gt;
  
  
  Abstraction
&lt;/h4&gt;

&lt;p&gt;Hiding information away from functions that should not be using that information is the practice of abstraction. Objects allow us to couple data and methods together in such a way that functions outside the object cannot use the data inside the object. This prevents misusing code written for a particular purpose that would lead to bugs in other parts of your program.&lt;/p&gt;

&lt;h4&gt;
  
  
  Encapsulation
&lt;/h4&gt;

&lt;p&gt;Encapsulation is the practice I alluded to earlier where you keep data and methods bundled together. Specifically, the data held in the object is only interacted with by methods within the same object. This allows for editing code in a class without creating issues with code outside of the class. Working to remove unnecessary connections between different parts of your code is known as decoupling and it leads to high cohesion in your code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Polymorphism
&lt;/h4&gt;

&lt;p&gt;This last aspect allows your code to be more readable. When creating subclasses from a particular superclass, you may want to use a method with the same name in each subclass but want it to differ depending on which subclass the object being called is an instance of. The method takes on many forms, known as polymorphism, to produce the desired result for the object that calls the method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Features of C++
&lt;/h2&gt;

&lt;p&gt;We are going to cover a few features of C++. I'll explain how each line of a simple program works to show how C++ handles different aspects of writing code. While I explain this code, I'll reference JavaScript as a comparison. This explanation will be a taste of C++, and if you're looking for a deeper explanation of the language, I recommend using &lt;a href="https://www.w3schools.com/cpp/cpp_intro.asp" rel="noopener noreferrer"&gt;W3 Schools&lt;/a&gt; where I learned everything I am about to go over. If you're short on time, Fireship's &lt;a href="https://www.youtube.com/watch?v=MNeX4EGtR5Y" rel="noopener noreferrer"&gt;"C++ in 100 Seconds"&lt;/a&gt; is about the fastest way you can see the different benefits of using C++ language in certain projects. Without further ado, let's check out some C++:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
using namespace std;

int hello(string name) {
    cout &amp;lt;&amp;lt; "Hello, " &amp;lt;&amp;lt; name &amp;lt;&amp;lt; endl;
    return 0;
}

int main()
{
    hello("Tyler");
    hello("Bob");
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a simple program written in C++. This is a popular coding example used to first explain C++, but I've modified it to show off a few more key features.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's start with the first line. This is known as a "Header File Library". In JavaScript, we would do something like this to import a library of functions, like underscore.js, to use instead of writing out the functions ourselves. If we want to use any of the objects from the standard library, we need to include them. This gives us the flexibility to only load the objects we need for the program we are writing. This particular library "iostream" contains objects that can read user inputs and output data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using namespace std;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a line of convenience code. Normally to use any object from the standard library, we would have to prefix it with &lt;code&gt;std::&lt;/code&gt;. On line 5, I make use of the &lt;code&gt;cout&lt;/code&gt; and &lt;code&gt;endl&lt;/code&gt; from the "iostream" standard library. Without this line of code, I would need to write &lt;code&gt;std::cout&lt;/code&gt; and &lt;code&gt;std::endl&lt;/code&gt; every time I use them. As your program gets larger, this means you'll be rewriting &lt;code&gt;std::&lt;/code&gt; many times just to use objects from the standard library, so we'll save ourselves a headache and write this one line of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int hello(string name) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is how you declare a function in C++. The function is named 'hello' and it has one parameter called 'name'. While this is similar to JavaScript, there are some noticeable differences. The only signifier that makes this a function is the parentheses after the function name and the code block that follows using braces. The other difference is the inclusion of the keywords &lt;code&gt;int&lt;/code&gt; and &lt;code&gt;string&lt;/code&gt;. Similar to TypeScript, these keywords indicate what values are expected from the function and the parameter. &lt;code&gt;int&lt;/code&gt; tells C++ that the function 'hello' should always return an integer, and if it doesn't, to either truncate a decimal point off to return an integer or throw an error to indicate a different value was returned. If you're not expecting to return anything, you should put &lt;code&gt;void&lt;/code&gt; instead. The &lt;code&gt;string&lt;/code&gt; keyword indicates that future arguments for this function need to be string data types or else C++ will throw an error. The 'hello' function will not run until it is declared, which I will go over soon.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    cout &amp;lt;&amp;lt; "Hello, " &amp;lt;&amp;lt; name &amp;lt;&amp;lt; endl;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the first statement of the function. Whenever you conclude a statement, you must always end it with a semicolon. JavaScript gives a little bit of leeway on this, but it cannot forgotten in C++. &lt;code&gt;cout&lt;/code&gt; is an object from the 'iostream' library used together with the insertion operator &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; to output the value to the right (to the terminal by default). We can chain outputs with more &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;, which allows us to use the name parameter in the output and include a line break with the &lt;code&gt;endl&lt;/code&gt; object from the 'iostream' library. When printing to the terminal, it is common practice to include &lt;code&gt;endl&lt;/code&gt; at the end to make sure each output to the terminal appears on a different line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By returning 0 at the end of the function, it satisfies the condition of &lt;code&gt;int&lt;/code&gt; needing an integer to be returned from 'hello'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int main()
{
    hello("Tyler");
    hello("Bob");
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;main()&lt;/code&gt; is a predefined function in C++ used to execute code. Any code inside its function body will be executed and there is no need to execute &lt;code&gt;main()&lt;/code&gt; later. A few common practices I've seen in C++ code: use &lt;code&gt;int&lt;/code&gt; and return 0 at the end of &lt;code&gt;main()&lt;/code&gt;, and put the entire code block below the function. Inside &lt;code&gt;main()&lt;/code&gt;, we call 'hello' twice with different arguments resulting in the following being printed to the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, Tyler
Hello, Bob
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final Thoughts on C++
&lt;/h2&gt;

&lt;p&gt;I first started looking into C++ because I heard it was used in game design. With the low memory usage, I can see how it is preferable when designing large games. The object-oriented nature of the language lends itself to naturally reusing more code and even including a custom library of objects for use on smaller applications for simple hardware. Finally, the requirement of including the data type for functions and variables leads to more thoughtful planning for your code as well as a safety net for errors when a data type accidentally gets changed. I look forward to exploring C++ further and I hope you check it out too!&lt;/p&gt;

&lt;p&gt;Happy Coding,&lt;br&gt;
Tyler Meyer&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;Sources: &lt;br&gt;
[1] &lt;a href="https://www.techtarget.com/searchdatamanagement/definition/C" rel="noopener noreferrer"&gt;https://www.techtarget.com/searchdatamanagement/definition/C&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[2] &lt;a href="https://en.wikipedia.org/wiki/Object-oriented_programming" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Object-oriented_programming&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[3] &lt;a href="https://www.w3schools.com/cpp/cpp_intro.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/cpp/cpp_intro.asp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[4] &lt;a href="https://www.youtube.com/watch?v=MNeX4EGtR5Y" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=MNeX4EGtR5Y&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>oop</category>
      <category>coding</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Test Driven Development:</title>
      <dc:creator>Tyler Meyer</dc:creator>
      <pubDate>Mon, 11 Nov 2024 04:55:35 +0000</pubDate>
      <link>https://dev.to/tymey/test-driven-development-d83</link>
      <guid>https://dev.to/tymey/test-driven-development-d83</guid>
      <description>&lt;h2&gt;
  
  
  What is TDD?
&lt;/h2&gt;

&lt;p&gt;Test Driven Development (TDD) is a technique that focuses on writing a test before writing code to pass the test. More traditional techniques would put writing code for a product first and then writing the test to see if the code previously written is performing as intended.&lt;/p&gt;

&lt;p&gt;While the efficacy of these two techniques can be debated, with both sides producing their own positives and negatives, I intend to show the structure of TDD and provide how it can create a workflow that leads to leaner code with fewer bugs at the end of a product's development cycle.&lt;/p&gt;

&lt;p&gt;Test Driven Development follows a cycle of understanding the request, writing a test, writing code to pass the test, refactoring your code, and then repeating this until you have your product.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5j1hc5tfx99s7g1swz8b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5j1hc5tfx99s7g1swz8b.png" alt="Image description" width="720" height="405"&gt;&lt;/a&gt;&lt;br&gt;
This figure is sourced from Grant Steinfeld's article on Test Driven Development: &lt;a href="https://developer.ibm.com/articles/5-steps-of-test-driven-development/" rel="noopener noreferrer"&gt;https://developer.ibm.com/articles/5-steps-of-test-driven-development/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With a focus on test writing, it may seem that this increases the time it takes to reach a final product. This is true, but if you consider making patches to your code after launch, studies have shown that teams using Test Driven Development techniques spend less time at this stage because there are fewer bugs and their code base is leaner. If you want to learn more about these studies, I highly recommend reading Ben Aston's article "Statistics &amp;amp; Studies: The Benefits Of Test Driven Development". &lt;a href="https://thectoclub.com/general/statistics-studies-benefits-test-driven-development/" rel="noopener noreferrer"&gt;Read here.&lt;/a&gt; &lt;/p&gt;
&lt;h3&gt;
  
  
  Understand the Request
&lt;/h3&gt;

&lt;p&gt;While this part of the process is not usually referenced when looking into TDD, I appreciate Grant Steinfeld's inclusion in his &lt;a href="https://developer.ibm.com/articles/5-steps-of-test-driven-development/" rel="noopener noreferrer"&gt;article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;By taking time to digest the request before you code, TDD becomes easier to begin and implement. The fine details are not as important as being able to talk about the project and the outputs that are expected. Consider making a loose plan as well to keep workflow focused.&lt;/p&gt;

&lt;p&gt;Let's consider a request for creating a doubly linked list data structure. This topic has been chosen for its familiarity with most developers.&lt;/p&gt;

&lt;p&gt;Request: "I need a class that creates a doubly linked list for storing data. The class needs methods to add data to the head or tail as well as remove data from the head or the tail. There should also be a method to check if a given piece of data is contained in the list. I have attached an image of what I am expecting."&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj4kddtj3tf4pcdwju75r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj4kddtj3tf4pcdwju75r.png" alt="Image description" width="800" height="243"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point, you should take time to think about the request. We have a class that needs methods to add and remove data. This data may need to be stored as an object to create the correct connections between the data. Now that we've taken time to think, we'll begin to code.&lt;/p&gt;
&lt;h3&gt;
  
  
  Write the Test
&lt;/h3&gt;

&lt;p&gt;Before coding for the request, write a test that would make your code fail the test. This performs the two tasks of confirming your test code is functional (by failing and not automatically passing) and creating a goal to reach.&lt;/p&gt;

&lt;p&gt;There are a couple of ways to write a test that will fail:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Write a test that references a function or property that doesn’t exist yet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Write a test that expects a certain value to be returned (that isn’t already being returned).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Typically, you'll want to begin writing your first test before you begin to code anything for the request. You'll notice that the first two tests are about making sure we have certain properties and methods in our doubly linked list class. For this article, we'll focus on the third test and beyond.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe('TDD Example -', function() {
  let doublyLinkedList;

  beforeEach(function() { // Perfrom this code before each test.
    doublyLinkedList = new DoublyLinkedList();
  });

  // Test #1: Properties
  it('should have a head and tail', function() { 
    expect(doublyLinkedList).to.have.property('head');
    expect(doublyLinkedList).to.have.property('tail');
  });

  // Test #2: Methods
  it('should have methods named addToTail, removeHead, contains, addToHead, &amp;amp; removeTail', function() {
    expect(doublyLinkedList.addToTail).to.be.a('function');
    expect(doublyLinkedList.removeHead).to.be.a('function');
    expect(doublyLinkedList.contains).to.be.a('function');
    expect(doublyLinkedList.addToHead).to.be.a('function');
    expect(doublyLinkedList.removeTail).to.be.a('function');
  });

  // Test #3: Adding data to the list at the tail.
  it('should designate a new tail when new nodes are added to tail with addToTail', function() {
    doublyLinkedList.addToTail(1); // Add data
    expect(doublyLinkedList.tail.value).to.equal(1); // Check for data
    doublyLinkedList.addToTail(2); // Add data
    expect(doublyLinkedList.tail.value).to.equal(2); // Check for data
    doublyLinkedList.addToTail(3); // Add data
    expect(doublyLinkedList.tail.value).to.equal(3); // Check for data
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5o5ofcb8rut5tsv1o1c5.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5o5ofcb8rut5tsv1o1c5.PNG" alt="Image description" width="787" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a test to check that data is being added to the tail property. I have included a property called value to be sure that I'm getting the data I added to the tail when I check for it. The test fails because the tail of my list points to null instead of any meaningful data. Now it's time to code!&lt;/p&gt;

&lt;h3&gt;
  
  
  Make it Pass
&lt;/h3&gt;

&lt;p&gt;Once you have the failing test, write code to pass the test. Keep it simple. Trying to think of every situation will distract you from the task at hand: passing the failing test. The code you write at this stage does not have to be elegant, so your goal should be to write as little code as possible.&lt;/p&gt;

&lt;p&gt;At this stage, you want to focus on writing as little code to pass the failing test you wrote into your test suite.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  addToTail(value) {
    // Create a class to store the value of the data.
    const newDoublyNode = new DoublyNode(value);
    // Reassign the tail of the list to the new node.
    this.tail = newDoublyNode;
  }

  removeHead() {}
  contains() {}
  addToHead() {}
  removeTail() {}
}

// Create a class to remember the value of the data stored in the list.
class DoublyNode {
  constructor(value) {
    this.value = value;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr262zmju0bxux1kn2a1q.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr262zmju0bxux1kn2a1q.PNG" alt="Image description" width="787" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! The test is now passing and we are one step closer to delivering a product that meets the expectations of our requester. Onto the refactoring stage.&lt;/p&gt;

&lt;h3&gt;
  
  
  Refactor Code
&lt;/h3&gt;

&lt;p&gt;Since the goals in previous iterations of the TDD cycle were to get a test to pass, the code base may become less efficient when it comes to time complexity, less organized and readable, and perhaps the code may stop following best practices. In regards to the above example, we did not write much code so the refactor stage is not warranted, but you should still consider this stage even when you write only a little code because you may be able to improve upon what you just created or have created before the test.&lt;/p&gt;

&lt;p&gt;Refactoring code after each test passes gives you the chance to engage with your code base, improve its implementation, and think of new ideas to improve upon the initial request without losing focus on the product you are producing. By having a wealth of tests in the test suite, every change made must adhere to the structure being developed with each cycle of TDD, which leads to fewer bugs when the project is completed as well as faster debugging when the need arises.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl0owap8dy8d7wnzzyce6.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl0owap8dy8d7wnzzyce6.PNG" alt="Image description" width="800" height="465"&gt;&lt;/a&gt;&lt;br&gt;
This figure is sourced from BEN ASTON's article "Statistics &amp;amp; Studies: The Benefits Of Test Driven Development: &lt;a href="https://thectoclub.com/general/statistics-studies-benefits-test-driven-development/" rel="noopener noreferrer"&gt;https://thectoclub.com/general/statistics-studies-benefits-test-driven-development/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The refactoring stage may be more involved because as you write new code or improve code that was written earlier, some of your older tests may fail.&lt;/p&gt;

&lt;p&gt;After finishing this cycle of TDD for this example, you may have several ideas to improve what I wrote: When you replace the tail each time, doesn't the previous tail get garbage collected? Shouldn't each piece of data have connections to the data next to it in the list?&lt;/p&gt;

&lt;p&gt;These are great questions and exactly what fuels Test Driven Development to be great for workflow.&lt;/p&gt;
&lt;h3&gt;
  
  
  Repeat
&lt;/h3&gt;

&lt;p&gt;After writing a test, passing the test, and refactoring code, you begin the cycle again by writing a new failing test that either improves upon the previous code written or introduces a new feature to the project.&lt;/p&gt;

&lt;p&gt;With every loop of Test Driven Development, the design and functionality of your code become leaner, the breadth of your test suite covers move details to catch bugs more quickly, and your understanding of the project becomes more familiar.&lt;/p&gt;

&lt;p&gt;“This workflow is sometimes called Red-Green-Refactoring, which comes from the status of the tests within the cycle.” –&lt;a href="https://developer.ibm.com/articles/5-steps-of-test-driven-development/" rel="noopener noreferrer"&gt;Grant Steinfeld&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's follow those questions from earlier and write a test to check that the nodes are connected in some way to avoid being garbage collected unnecessarily.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it('should return the value of the node previous of the tail', function() {
    doublyLinkedList.addToTail(1);
    doublyLinkedList.addToTail(2);
    expect(doublyLinkedList.tail.previous.value).to.equal(1);

    // Add more nodes and test again
    doublyLinkedList.addToTail(3);
    doublyLinkedList.addToTail(4);
    doublyLinkedList.addToTail(5);
    expect(doublyLinkedList.tail.previous.value).to.equal(4);
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr2udd52jdh8spz289e68.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr2udd52jdh8spz289e68.PNG" alt="Image description" width="785" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When observing the tests I wrote, you'll notice I included a "previous" property on the data node. I wrote this because I intend for the data node itself to contain the connection between data nodes just like the picture shown in the request. The test fails because my data nodes have no "previous" property.&lt;/p&gt;

&lt;p&gt;In order to wrap up this article (and to not keep you for the many iterations of the Test Driven Development cycle needed to complete this request), I will stop my example here. Feel free to use this starting point to create your own Doubly Linked List. Practicing the Test Driven Development cycle makes this approach less intimidating, and I am certain that you'll appreciate the result of the product you produce by the end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Test Driven Development encourages programmers to engage with their code base more mindfully since they are expanding the test suite while they develop their code for a request. When considering this technique, do not let the idea of a longer development timeline dissuade you from trying. The reports of saving time after a product launch should be encouraging because you will have more time to work on future projects. In Ben Aston's research on &lt;a href="https://thectoclub.com/general/statistics-studies-benefits-test-driven-development/" rel="noopener noreferrer"&gt;Test Driven Development&lt;/a&gt;, he noted that "as programmers get better at TDD, they are likely to move faster."&lt;/p&gt;

&lt;p&gt;Consider Test Driven Development on your next project!&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;br&gt;
Tyler Meyer&lt;/p&gt;

&lt;h4&gt;
  
  
  Sources:
&lt;/h4&gt;

&lt;p&gt;5 Steps of Test-Driven Development by Grant Steinfeld:&lt;br&gt;
&lt;a href="https://developer.ibm.com/articles/5-steps-of-test-driven-development/" rel="noopener noreferrer"&gt;https://developer.ibm.com/articles/5-steps-of-test-driven-development/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Statistics &amp;amp; Studies: The Benefits Of Test-Driven Development by Ben Aston:&lt;br&gt;
&lt;a href="https://thectoclub.com/general/statistics-studies-benefits-test-driven-development/" rel="noopener noreferrer"&gt;https://thectoclub.com/general/statistics-studies-benefits-test-driven-development/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>testing</category>
      <category>programming</category>
      <category>productivity</category>
      <category>development</category>
    </item>
    <item>
      <title>Flexible Uses of the Logical AND (&amp;&amp;) and OR (||) in JavaScript</title>
      <dc:creator>Tyler Meyer</dc:creator>
      <pubDate>Wed, 25 Sep 2024 21:25:26 +0000</pubDate>
      <link>https://dev.to/tymey/flexible-uses-of-the-logical-and-and-or-in-javascript-1c6o</link>
      <guid>https://dev.to/tymey/flexible-uses-of-the-logical-and-and-or-in-javascript-1c6o</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Boolean values are absolute, &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. That’s as clear cut as it gets. Other data types in JavaScript also have these inherent values of &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;, but it isn’t as obvious because they look like &lt;code&gt;32&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt;, and &lt;code&gt;‘Hello’&lt;/code&gt; instead of &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;. Knowing that all values have these inherent values means that we can perform operations on all data types that are typically used for Booleans. This provides us more creativity and flexibility while coding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Truthy and Falsy Values
&lt;/h2&gt;

&lt;p&gt;When working with control flow keywords like &lt;code&gt;if&lt;/code&gt; and logical operators like AND (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;) and OR (&lt;code&gt;||&lt;/code&gt;), we use Booleans to achieve certain outcomes. These Booleans can be used explicitly with &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;, but we often generate them with comparison operators such as &lt;code&gt;===&lt;/code&gt;, &lt;code&gt;&amp;lt;&lt;/code&gt;, and &lt;code&gt;&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What happens if we don’t use a Boolean with control flow or logical operators? Well, you’re in luck! All values are inherently true or false to help with this. We can categorize all values into two categories: &lt;strong&gt;truthy&lt;/strong&gt; or &lt;strong&gt;falsy&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When trying to figure out if a value is truthy or falsy, it is best to remember the &lt;strong&gt;falsy&lt;/strong&gt; values since there are only a limited amount of them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;false&lt;/code&gt; (a Boolean value)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;null&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;NaN&lt;/code&gt; (not a number)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt; (a number value)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;""&lt;/code&gt; (the empty string value)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything else is &lt;strong&gt;truthy&lt;/strong&gt;. If you are unsure if something is truthy or falsy or you come across a unique situation that seems ambiguous, you can always create an &lt;code&gt;if&lt;/code&gt; statement to see if the code inside the following code block runs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (23) { console.log(“truthy”); } // Prints “truthy”
else { console.log(“falsy”); }

if (null) { console.log(“truthy”); } 
else { console.log(“falsy”); } // Prints “falsy”
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Logical AND (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;)
&lt;/h2&gt;

&lt;p&gt;When using Booleans with the Logical AND (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;), both values need to be &lt;code&gt;true&lt;/code&gt; in order for the logical operator to return &lt;code&gt;true&lt;/code&gt;. Otherwise, if at least one value is &lt;code&gt;false&lt;/code&gt;, it will return &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(false &amp;amp;&amp;amp; false); // false
console.log(true &amp;amp;&amp;amp; false); // false
console.log(true &amp;amp;&amp;amp; true); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Understanding the mechanics of the logical AND (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;) operator can help you when it comes to truthy and falsy values. If the value on the left is false, return it; otherwise, return the value on the right.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(0 &amp;amp;&amp;amp; 1); // 0
console.log("a" &amp;amp;&amp;amp; ""); // "" (an empty string)
console.log([] &amp;amp;&amp;amp; [1, 2, 3]); // [1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The logical AND (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;) operator wants to return a falsy value and only returns the truthy value on the right if both are truthy. You can think of the two arguments like this:&lt;/p&gt;

&lt;p&gt;(Left Side) Only use me if I am a falsy value.  &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;  (Right Side) Otherwise, use me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Logical OR (&lt;code&gt;||&lt;/code&gt;)
&lt;/h2&gt;

&lt;p&gt;When using Booleans with the Logical OR (&lt;code&gt;||&lt;/code&gt;), both values need to be &lt;code&gt;false&lt;/code&gt; in order for the logical operator to return &lt;code&gt;false&lt;/code&gt;. Otherwise, if at least one value is &lt;code&gt;true&lt;/code&gt;, it will return &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(false || false); // false
console.log(true || false); // true
console.log(true || true); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s how the logical OR (&lt;code&gt;||&lt;/code&gt;) operator works: if the value on the left is true, return it; otherwise, return the value on the right.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(1 || 0); // 1
console.log("" || "a"); // "a"
console.log(undefined || null); // null
console.log([] || [1, 2, 3]); // []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The logical OR (&lt;code&gt;||&lt;/code&gt;) operator wants to return a truthy value and only returns the falsy value on the right if both are falsy. You can think of the two arguments like this:&lt;/p&gt;

&lt;p&gt;(Left Side) Only use me if I am a truthy value.  &lt;code&gt;||&lt;/code&gt;  (Right Side) Otherwise, use me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creative Uses of AND (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;) and OR (&lt;code&gt;||&lt;/code&gt;)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Using a Default Value When Expecting an Input
&lt;/h3&gt;

&lt;p&gt;Let’s say you’re creating an object that represents a person that comes with properties that describes the person along with a function that greets others using the other properties in the object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Person(name) {
    // If name is undefined, this.name will 
    // default to 'a person with no name'
    this.name = name || 'a person with no name';
    this.greet = function() {
        console.log('Hello, I am ' + this.name + '.');
    };
}

// Create Person variables
var tyler = new Person('Tyler');
var mystery = new Person(); 
// Without an input, this.name defaults to the 
// second option since name is undefined.

// Call greet() from each Person object
tyler.greet(); // "Hello, I am Tyler."
mystery.greet(); // "Hello, I am a person with no name."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we expected an input for the &lt;code&gt;name&lt;/code&gt; parameter, so the second value in the OR (&lt;code&gt;||&lt;/code&gt;) operation is only used if &lt;code&gt;name&lt;/code&gt; is undefined (no argument at function call).&lt;/p&gt;

&lt;h3&gt;
  
  
  Requiring Multiple Inputs
&lt;/h3&gt;

&lt;p&gt;If you are creating objects and want to make sure you have a set number of inputs before you make the object, you can chain together logical AND (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;) operators across each required parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Person(firstName, lastName, age) {
  if (firstName &amp;amp;&amp;amp; lastName &amp;amp;&amp;amp; age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.fullName = `${this.firstName} ${this.lastName}`;
    this.age = age;
    this.greet = function() {
      console.log(`Hello, my name is ${this.fullName} and I'm ${this.age} years old.`);
    };
  } 

  // If any argument is missing, the object will only have this property.
  else {
    this.greet = function() {
      console.log(`Hello, I am not a fully formed Person.`)
    };
  }
}

var tyler = new Person('Tyler', 'Meyer', 32);
var brad = new Person('Brad', '', 38);

tyler.greet(); // "Hello, my name is Tyler Meyer and I'm 32 years old."
brad.greet(); // "Hello, I am not a fully formed Person."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;if&lt;/code&gt; statement is checking for an argument for each parameter before it creates the full Person object. If even one argument is a falsy value, it will create an object with the else statement instead. Therefore, we can prevent incomplete objects or create default objects for incomplete entries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If you need a default value until a value is supplied, the logical OR (&lt;code&gt;||&lt;/code&gt;) operator can be very helpful. If you need to require multiple values before proceeding, the logical AND (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;) operator can be very helpful. These are just two examples, and as you continue to explore these operators, you will find out there are many more ways to use these operators outside the usual checking for &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; Boolean values. Keep these two things in mind when looking into using the logical AND (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;) and OR (&lt;code&gt;||&lt;/code&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;OR&lt;/strong&gt; (&lt;code&gt;||&lt;/code&gt;) : Only use the value on the left if it is &lt;strong&gt;truthy&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AND&lt;/strong&gt; (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;) : Only use the value on the left if it is &lt;strong&gt;falsy&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have any questions, please leave them in the comments. I'd be happy to discuss this topic further.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>learning</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
