<?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>Forem: Diego (Relatable Code)</title>
    <description>The latest articles on Forem by Diego (Relatable Code) (@diballesteros).</description>
    <link>https://forem.com/diballesteros</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%2F691335%2F61351551-464a-4150-9246-c2fca636d78d.png</url>
      <title>Forem: Diego (Relatable Code)</title>
      <link>https://forem.com/diballesteros</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/diballesteros"/>
    <language>en</language>
    <item>
      <title>How to set up Bun on a Windows Computer</title>
      <dc:creator>Diego (Relatable Code)</dc:creator>
      <pubDate>Tue, 25 Oct 2022 11:32:00 +0000</pubDate>
      <link>https://forem.com/diballesteros/how-to-set-up-bun-on-a-windows-computer-251d</link>
      <guid>https://forem.com/diballesteros/how-to-set-up-bun-on-a-windows-computer-251d</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Every day it feels like the JavaScript ecosystem undergoes changes. The introduction of Bun is just further proof of that and boy is it powerful.&lt;/p&gt;

&lt;p&gt;But, what is Bun?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F242%2F0%2ATn2c5qBp61DARX1R" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F242%2F0%2ATn2c5qBp61DARX1R" alt="Bun"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Bun is a fast all-in-one JavaScript runtime&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Source: &lt;a href="https://bun.sh/" rel="noopener noreferrer"&gt;https://bun.sh/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What makes it “all-in-one” though? Well, let’s just head one line down for their personal description of what makes it stand out amongst the competition.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Bundle, transpile, install and run JavaScript &amp;amp; TypeScript projects — all in Bun. Bun is a new JavaScript runtime with a native bundler, transpiler, task runner and npm client built-in.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is a ton of functionality baked (heh) into one runtime. One of the things that caught my eye was that it also automatically transpiles files. That means you can just use Typescript and JSX with &lt;strong&gt;no setup&lt;/strong&gt;  needed.&lt;/p&gt;

&lt;p&gt;That sort of functionality truly does feel like it should be the norm. These tools are tried-and-tested and them &lt;strong&gt;just&lt;/strong&gt; working feels right.&lt;/p&gt;

&lt;p&gt;However, I tend to use my Windows computer a lot because I tend to game on the side. For convenience’s sake, I also do a lot of coding on my Windows computer (where applicable). So I decided to give Bun a shot whilst on my Windows computer.&lt;/p&gt;

&lt;p&gt;Let’s dive right in on how to set it up.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up WSL
&lt;/h3&gt;

&lt;p&gt;In order for us to install certain functionalities we need to set up a Linux distribution on our Windows machine. Thankfully, the developers at Microsoft have created something called the Windows Subsystem for Linux (WSL)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The Windows Subsystem for Linux lets developers run a GNU/Linux environment — including most command-line tools, utilities, and applications — directly on Windows, unmodified, without the overhead of a traditional virtual machine or dualboot setup.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you want more info &lt;a href="https://learn.microsoft.com/en-us/windows/wsl/" rel="noopener noreferrer"&gt;check out the full docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is exactly what we’re looking for to set up Bun.&lt;/p&gt;

&lt;p&gt;Open up the Windows Powershell (with admin privileges) and run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wsl &lt;span class="nt"&gt;--install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll probably get a line of messages similar to this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F908%2F0%2AHx8zdN3jrjuiDmPl" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F908%2F0%2AHx8zdN3jrjuiDmPl" alt="messages"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome! Let’s restart our machine.&lt;/p&gt;

&lt;p&gt;After restarting the terminal will automatically attempt to run some commands. However, you may get the following error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Installing, this may take a few minutes... 
WslRegisterDistribution failed with error 0x80370102 
Error: 0x80370102 The virtual machine could not be started because a required feature is not installed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, virtualization may not be enabled on your PC. How to enable this can vary from processor to processor so I just did a quick google search. In my case, I had an AMD Ryzen, so I had to reboot and enable SVM in my BIOS settings.&lt;/p&gt;

&lt;p&gt;After enabling open up another terminal and run the following command to re-install the Linux distribution. If it previously worked you can skip this step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wsl &lt;span class="nt"&gt;--install&lt;/span&gt; &lt;span class="nt"&gt;-b&lt;/span&gt; Ubuntu
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will install and ask you to create a UNIX user account.&lt;/p&gt;

&lt;h4&gt;
  
  
  Installing Docker Desktop
&lt;/h4&gt;

&lt;p&gt;It’s &lt;strong&gt;recommended&lt;/strong&gt; to use Docker Desktop with WSL. Let’s go ahead and get that set up.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.docker.com/products/docker-desktop/" rel="noopener noreferrer"&gt;https://www.docker.com/products/docker-desktop/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Download the executable and install Docker Desktop.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F429%2F0%2A7-TjnkC_xU-McLZX" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F429%2F0%2A7-TjnkC_xU-McLZX" alt="docker desktop installing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will require a logout. Afterward, head into settings and make sure Use the WSL 2-based engine is enabled.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F532%2F0%2AneDWkZaidyT0vtSl" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F532%2F0%2AneDWkZaidyT0vtSl" alt="docker desktop settings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Resources, under WSL Integration make sure your installed distro is enabled as well.&lt;/p&gt;

&lt;p&gt;For more detailed instructions and troubleshooting &lt;a href="https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-containers" rel="noopener noreferrer"&gt;check out the WSL docs&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Bun on Windows
&lt;/h3&gt;

&lt;p&gt;That was quite a bit of prep just to use Bun. Grab a coffee and come back so we can continue!&lt;/p&gt;

&lt;h4&gt;
  
  
  Installing Bun
&lt;/h4&gt;

&lt;p&gt;Open up the Ubuntu (just search up Ubuntu from the Start menu and it should appear if it was installed correctly) instance and install unzip.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;unzip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And install bun&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl https://bun.sh/install | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F660%2F0%2ADDGFiS7AGy0gXY9U" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F660%2F0%2ADDGFiS7AGy0gXY9U" alt="commands"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One more step. We have to add a few lines manually to our .bashrc&lt;/p&gt;

&lt;p&gt;Now you have full access to the bun commands! Restart the Ubuntu terminal and run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bun &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should confirm if it was correctly installed or not.&lt;/p&gt;

&lt;h4&gt;
  
  
  Testing out Bun
&lt;/h4&gt;

&lt;p&gt;I’ll be using Vscode to simplify the process but it’s not necessary. Vscode has an extension available to integrate it with WSL easily.&lt;/p&gt;

&lt;p&gt;Here are their docs for more info:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://code.visualstudio.com/docs/remote/wsl" rel="noopener noreferrer"&gt;https://code.visualstudio.com/docs/remote/wsl&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Under extensions just look up WSL to install the respective extension&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F274%2F0%2AwUCeTPYSn6NqoInI" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F274%2F0%2AwUCeTPYSn6NqoInI" alt="WSL extension"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome!&lt;/p&gt;

&lt;p&gt;To test this out create a folder with the name http.js and add the following lines to the file (note this is straight from the docs):&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome to Bun!&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the folder with vscode. In the bottom left of the vscode screen you should see a button with two arrows&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F34%2F0%2A4Y2X2JCOoG6L0I9l" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F34%2F0%2A4Y2X2JCOoG6L0I9l" alt="arrows"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click it and select the option to re-open the folder with WSL. Your terminal should now be connected to the Ubuntu instance. From here run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bun run http.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;localhost:3000&lt;/a&gt; you’ll now see the executed file!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F257%2F0%2AgR2_Q6BPvnORM96c" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F257%2F0%2AgR2_Q6BPvnORM96c" alt="executed file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping it up
&lt;/h3&gt;

&lt;p&gt;That was quite the setup but now we can do more complex things with Bun. In the next article, I’ll go over how to set up a more complex project.&lt;/p&gt;

&lt;p&gt;Do note that Bun is still in Beta so you may encounter some issues! They have an active discord so I recommend checking them out if you run into any problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s Connect
&lt;/h3&gt;

&lt;p&gt;If you liked this feel free to connect with me on &lt;a href="https://www.linkedin.com/in/relatablecode" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/relatablecoder" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out my free developer roadmap and weekly tech industry news in my &lt;a href="https://relatablecode.substack.com/" rel="noopener noreferrer"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>windows</category>
      <category>react</category>
    </item>
    <item>
      <title>Testing a React Application Working with Codecov</title>
      <dc:creator>Diego (Relatable Code)</dc:creator>
      <pubDate>Tue, 18 Oct 2022 11:04:52 +0000</pubDate>
      <link>https://forem.com/diballesteros/testing-a-react-application-working-with-codecov-55n</link>
      <guid>https://forem.com/diballesteros/testing-a-react-application-working-with-codecov-55n</guid>
      <description>&lt;p&gt;In the previous article, we began our setup for &lt;a href="https://about.codecov.io/"&gt;Codecov&lt;/a&gt; by instrumenting our codebase. In this final article, we’ll finish our setup with a GitHub workflow that will automatically submit our coverage results.&lt;/p&gt;

&lt;p&gt;Just to give a small recap this is what code coverage is and what our application does:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Code coverage is a software testing metric that determines the number of lines of code that is successfully validated under a test procedure, which in turn, helps in analyzing how comprehensively a software is verified.&lt;/em&gt; &lt;a href="https://www.codegrip.tech/productivity/everything-you-need-to-know-about-code-coverage/"&gt;&lt;em&gt;Source&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Cool! Next, let’s go over what our application does. When clicking on a button it fetches posts from a service and displays them on the page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kIonv26h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/593/0%2AqRlKI52IcTUhNiDM" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kIonv26h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/593/0%2AqRlKI52IcTUhNiDM" alt="Application" width="593" height="675"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You find the full repository &lt;a href="https://github.com/diballesteros/react-testing"&gt;github.com/diballesteros/react-testing&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a GitHub workflow
&lt;/h3&gt;

&lt;p&gt;Let’s check out the official docs from GitHub for a clear definition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A workflow is a configurable automated process that will run one or more jobs. Workflows are defined by a YAML file checked in to your repository and will run when triggered by an event in your repository, or they can be triggered manually, or at a defined schedule&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s a lot to take in. Essentially we can create a folder at the root directory of our project that Github will &lt;strong&gt;automatically&lt;/strong&gt; detect to implement the “workflow”. Magic!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UhGRDaf2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2A8qkTwUirK05hmiN3" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UhGRDaf2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2A8qkTwUirK05hmiN3" alt="Workflow" width="880" height="162"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These actions can be monitored in the Actions tab in a repository.&lt;/p&gt;

&lt;p&gt;Awesome! But how does this help us? Well, we’re going to create our very own &lt;a href="https://yaml.org/"&gt;YAML&lt;/a&gt; file to create a workflow that will trigger on every push to our repository. This way our code coverage will always stay up to date.&lt;/p&gt;

&lt;p&gt;To find out more about workflows and GitHub actions check out the &lt;a href="https://docs.github.com/en/actions/using-workflows/about-workflows"&gt;official docs&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating our workflow
&lt;/h3&gt;

&lt;p&gt;In the root directory of our project create a .github folder and inside of that create a workflows folder with a single main.yml file inside.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qi1B5gMf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/207/0%2AiSaNLuY1J0JHRw6R" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qi1B5gMf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/207/0%2AiSaNLuY1J0JHRw6R" alt="file structure" width="207" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;YAML files have a fairly simple structure that is very dependent on the spacing of each line to know what the next step is. Highly recommend checking out their docs if you’re not too familiar with them.&lt;/p&gt;

&lt;p&gt;Let’s start off by giving our workflow a name and making it trigger on push.&lt;/p&gt;

&lt;p&gt;Afterward, we can start setting up our steps. Let’s think about what this implies. GitHub is spinning up an environment to run our tests. This means that in this environment we must have all the necessary dependencies for Cypress to run correctly.&lt;/p&gt;

&lt;p&gt;This may seem complicated at first, but it’ll soon make sense.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wun0SkYD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/503/0%2AACeL10Th7yQOoF9n" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wun0SkYD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/503/0%2AACeL10Th7yQOoF9n" alt="order of actions" width="503" height="513"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can start adding jobs to our workflow file to do just this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;tests&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
        &lt;span class="na"&gt;container&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cypress/browsers:node14.17.0-chrome88-ff89&lt;/span&gt;
        &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;🛫 Checkout&lt;/span&gt;
              &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v3&lt;/span&gt;

            &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;🌲 Cypress install&lt;/span&gt;
              &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cypress-io/github-action@v4&lt;/span&gt;
              &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
                  &lt;span class="na"&gt;runTests&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
            &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm run build&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;runs-on&lt;/code&gt; specifies the environment we'll use to install our dependencies. To make it simple we'll just use the latest version of Ubuntu that is available.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;container&lt;/code&gt; is the browser.&lt;/p&gt;

&lt;p&gt;The Checkout step gets our code from the repo. Then we install Cypress.&lt;/p&gt;

&lt;p&gt;To add to this we’ll use some actions directly from Cypress to run our tests and upload them to Codecov.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;tests&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
        &lt;span class="na"&gt;container&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cypress/browsers:node14.17.0-chrome88-ff89&lt;/span&gt;
        &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;🛫 Checkout&lt;/span&gt;
              &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v3&lt;/span&gt;

            &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;🌲 Cypress install&lt;/span&gt;
              &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cypress-io/github-action@v4&lt;/span&gt;
              &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
                  &lt;span class="na"&gt;runTests&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
            &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm run build&lt;/span&gt;

            &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;💻 'UI Tests'&lt;/span&gt;
              &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cypress-io/github-action@v4&lt;/span&gt;
              &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
                  &lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm run dev&lt;/span&gt;
                  &lt;span class="na"&gt;wait-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;http://localhost:3000&lt;/span&gt;
                  &lt;span class="na"&gt;wait-on-timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;120&lt;/span&gt;
                  &lt;span class="na"&gt;browser&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;chrome&lt;/span&gt;
                  &lt;span class="na"&gt;spec&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;cypress/integration/first-test.spec.js'&lt;/span&gt;

            &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;✅ Upload coverage to Codecov&lt;/span&gt;
              &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;codecov/codecov-action@v3.1.0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;UI Tests&lt;/code&gt; will look for the GitHub action directly from Cypress to run our test in a chrome browser. Something very important to note is that in the spec property we specified only the test we created: &lt;code&gt;first-test.spec.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can use an * to capture all files. For example: cypress/integration/*.&lt;/p&gt;

&lt;p&gt;Our final step will upload it to Codecov using a GitHub action they have provided for us.&lt;/p&gt;

&lt;h3&gt;
  
  
  The GitHub Action
&lt;/h3&gt;

&lt;p&gt;After committing and pushing up the new changes we’ll finally be able to test it out. If you head to the repository and go to the GitHub Actions tab you’ll see the action automatically running:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1Impdr_c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/525/0%2AW6cWGqK-K2nv4xjp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1Impdr_c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/525/0%2AW6cWGqK-K2nv4xjp" alt="github action running" width="525" height="130"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you click on the action you’ll be able to see a detailed view of what tasks are being run and the steps.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CjzL5tHA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/963/0%2AdAiN6HRm2f4KXqoe" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CjzL5tHA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/963/0%2AdAiN6HRm2f4KXqoe" alt="github action steps" width="880" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the screenshot above I made a mistake with the &lt;code&gt;run&lt;/code&gt; command for the project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping it up
&lt;/h3&gt;

&lt;p&gt;We’ll now be able to see the results in Codecov in our project list:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lo2JYxoo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2AH8fdcAzS_8W91ke9" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lo2JYxoo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2AH8fdcAzS_8W91ke9" alt="codecov results" width="880" height="78"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ll also see how much coverage we actually have:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--woBTqEnh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/319/0%2A01XXy6vX1PpiWF84" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--woBTqEnh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/319/0%2A01XXy6vX1PpiWF84" alt="codecov details" width="319" height="109"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also see what lines are not being covered:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T4tVt5wX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/459/0%2A7QGM1eXHYQjLQTd3" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T4tVt5wX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/459/0%2A7QGM1eXHYQjLQTd3" alt="codecov coverage" width="459" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out the official Codecov docs for more info on how to navigate the dashboard:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.codecov.com/docs"&gt;https://docs.codecov.com/docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That wraps up my series. I hope you enjoyed it! Testing is a fundamental part of the software development process and is something that should not be skimmed over.&lt;/p&gt;

&lt;p&gt;Thankfully modern-day testing has a plethora of tools that make our job easier. If you have any more questions regarding testing leave them in the comments section below!&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s Connect
&lt;/h3&gt;

&lt;p&gt;If you liked this feel free to connect with me on &lt;a href="https://www.linkedin.com/in/relatablecode"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/relatablecoder"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out my free developer roadmap and weekly tech industry news in my &lt;a href="https://relatablecode.substack.com/"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>testing</category>
      <category>react</category>
    </item>
    <item>
      <title>Testing a React Application Instrumenting the codebase</title>
      <dc:creator>Diego (Relatable Code)</dc:creator>
      <pubDate>Mon, 10 Oct 2022 14:05:08 +0000</pubDate>
      <link>https://forem.com/diballesteros/testing-a-react-application-instrumenting-the-codebase-1nj9</link>
      <guid>https://forem.com/diballesteros/testing-a-react-application-instrumenting-the-codebase-1nj9</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;In the previous article, we went over how to update Cypress to its amazing version 10! Now we’ll finish off this series by adding code coverage to our application and incorporating it into our CI. But what is code coverage?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Code coverage is a software testing metric that determines the number of lines of code that is successfully validated under a test procedure, which in turn, helps in analyzing how comprehensively a software is verified.&lt;/em&gt; &lt;a href="https://www.codegrip.tech/productivity/everything-you-need-to-know-about-code-coverage/"&gt;&lt;em&gt;Source&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In most cases, this is a percentage of how much the codebase is being covered by tests. How do we know if a test covers a certain amount of code? Instrumentation of our code monitors and traces the test coverage and gives us accurate reports.&lt;/p&gt;

&lt;p&gt;Cool! First, let’s go over what our application does. When clicking on a button it fetches posts from a service and displays them on the page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6m8850DE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/593/0%2AgmHy6RMEL65jjvgB" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6m8850DE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/593/0%2AgmHy6RMEL65jjvgB" alt="applicationj" width="593" height="675"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For reference, you can find the repository &lt;a href="https://github.com/diballesteros/react-testing"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing codecov
&lt;/h3&gt;

&lt;p&gt;We will be using &lt;a href="https://about.codecov.io/"&gt;Codecov&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Ship healthier code faster with less risk.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s their motto! But how do we go about using it? In the link above make sure to make an account and connect your repository.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x0I2dA91--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2Aif-2MxPm3cfJzds4" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x0I2dA91--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2Aif-2MxPm3cfJzds4" alt="codecov" width="880" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the Not yet setup tab we should find our repository.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--U0Ff9BIi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/996/0%2Aborvah9sEJFUhA86" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U0Ff9BIi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/996/0%2Aborvah9sEJFUhA86" alt="not yet setup tab" width="880" height="45"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will give us some brief instructions on how to set up code coverage with our repository. First, let’s install a necessary Cypress package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @cypress/code-coverage &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then in our cypress/support/e2e.js file let's go ahead and import this new code.&lt;/p&gt;

&lt;p&gt;In our cypress.config.js we also have to set up the events to correctly execute the code coverage:&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cypress&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&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="nx"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;e2e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;baseUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:3000&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;specPattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cypress/integration/**/*.spec.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;setupNodeEvents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@cypress/code-coverage/task&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;config&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;config&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s open up our application and cypress to run our test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run dev 

npm run test-e2e
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we run our test we will see the following command:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ynd-UBdq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/429/0%2ARnLu4myu8jCw1OzK" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ynd-UBdq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/429/0%2ARnLu4myu8jCw1OzK" alt="log command" width="429" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we’re missing is the instrumentation for our code. There are a few ways to go about this. But first, let’s add the two new folders to our .gitignore:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Code coverage files 
coverage 
.nyc_output
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To instrument our code we have to install the following plugin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;vite-plugin-istanbul &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our vite.config.js we should import the newly installed library and set it up:&lt;/p&gt;

&lt;p&gt;Some interesting notes on how the Istanbul plugin works can be found &lt;a href="https://github.com/iFaxity/vite-plugin-istanbul"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, when we run the rest we’ll no longer see the same warning as before:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C1st_9eb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/425/0%2AtVNXmX1IEDcjiz1P" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C1st_9eb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/425/0%2AtVNXmX1IEDcjiz1P" alt="no warning" width="425" height="567"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get a better understanding of what’s going on let’s take a look at the root directory you’ll see a folder, coverage:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lS5Pfjdb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/279/0%2A4seHbZPH582ATOq7" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lS5Pfjdb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/279/0%2A4seHbZPH582ATOq7" alt="coverage files" width="279" height="137"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You’ll see some JSON files that detail exactly what is covered in the files of the directory we provided earlier in the vite.config.js (./src).&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping it up
&lt;/h3&gt;

&lt;p&gt;Instrumenting our code base is crucial to providing Codecov with the necessary information it needs to give us the percentage of code coverage we actually have.&lt;/p&gt;

&lt;p&gt;In the next article, I’ll go over how to set up the GitHub Action to automatically send the code coverage report to CodeCov with every push. This way you don’t have to think about it at all!&lt;/p&gt;

&lt;p&gt;You can find the full code here at this &lt;a href="https://github.com/diballesteros/react-testing"&gt;repo&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s Connect
&lt;/h3&gt;

&lt;p&gt;If you liked this feel free to connect with me on &lt;a href="https://www.linkedin.com/in/relatablecode"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/relatablecoder"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out my free developer roadmap and weekly tech industry news in my &lt;a href="https://relatablecode.substack.com/"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to migrate Zustand from Version 3 to 4</title>
      <dc:creator>Diego (Relatable Code)</dc:creator>
      <pubDate>Tue, 04 Oct 2022 00:19:02 +0000</pubDate>
      <link>https://forem.com/diballesteros/how-to-migrate-zustand-from-version-3-to-4-548b</link>
      <guid>https://forem.com/diballesteros/how-to-migrate-zustand-from-version-3-to-4-548b</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Zustand is an awesome state management library for React that excels in its simplicity. It's just one hook that can then be called from anywhere in the app to access your  &lt;strong&gt;store&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This simplicity has made it the perfect choice for personal small-medium sized apps because it requires little boilerplate and it comes baked in with some awesome middleware!&lt;/p&gt;

&lt;p&gt;As an aside middleware is:&lt;/p&gt;

&lt;p&gt;such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Persisting to local storage&lt;/li&gt;
&lt;li&gt;Immer to make state changes easier to handle&lt;/li&gt;
&lt;li&gt;Store version and migration handling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I used this library to handle persisted data in a personal app of &lt;a href="https://nuzlocke.netlify.app/" rel="noopener noreferrer"&gt;mine&lt;/a&gt;. Essentially it's a CRUD app to manage Pokemon you run into in a playthrough of a game.&lt;/p&gt;

&lt;p&gt;The important part however is that I have an &lt;strong&gt;obsession&lt;/strong&gt; with keeping my libraries up-to-date (specifically in these small personal apps) so I decided to go ahead and migrate Zustand from version 3 to 4.&lt;/p&gt;

&lt;p&gt;As a small disclaimer, this article assumes you have some base-level experience with Zustand.&lt;/p&gt;

&lt;h3&gt;
  
  
  Migration Process
&lt;/h3&gt;

&lt;p&gt;First and foremost you can check out the changelog and any specific changes you may need to do on the &lt;a href="https://github.com/pmndrs/zustand/releases" rel="noopener noreferrer"&gt;Zustand GitHub repo changelog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;First, let’s go ahead and update the library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add zustand
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In my case, it updated to 4.1.1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F206%2F0%2A02lwuJvSKqlz2iB1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F206%2F0%2A02lwuJvSKqlz2iB1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All the previous APIs are backward compatible so keep in mind that the changes I’m going to make apply to Typescript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://immerjs.github.io/immer/" rel="noopener noreferrer"&gt;Immer&lt;/a&gt; is a library that allows you to work with immutable state data in a more convenient way. Before we had to explicitly set the type like this and the function to use it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const immer =
  &amp;lt;T extends State&amp;gt;(config: StateCreator&amp;lt;T&amp;gt;): StateCreator&amp;lt;T&amp;gt; =&amp;gt;
  (set, get, api) =&amp;gt;
    config(
      (partial, replace) =&amp;gt; {
        const nextState =
          typeof partial === 'function' ? produce(partial as (state: T) =&amp;gt; T) : (partial as T);
        return set(nextState, replace);
      },
      get,
      api
    );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, we can now just import it directly from Zustand’s middleware, so we can delete the above code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { immer } from 'zustand/middleware/immer';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, the section where you’re creating the store requires a small change:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const useStore = create&amp;lt;AppState&amp;gt;(
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const useStore = create&amp;lt;AppState&amp;gt;()(
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is now treated as a curried function.&lt;/p&gt;

&lt;p&gt;Last but not least, I used the migrate option in the PersisOptions that Zustand provides to persist all data to local storage. However, the state of this app is now considered unknown by Typescript.&lt;/p&gt;

&lt;p&gt;We can fix this by explicitly declaring the type with an assert function. For the sake of brevity I did something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;migrate: (persistedState, version) =&amp;gt; { assertAppState(persistedState);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The assert function looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function assertAppState(val: unknown): asserts val is AppState {
  if (typeof val !== 'object') {
    throw new TypeError('Invalid app state');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means anytime I call my state object afterward in the migrate function then it will have the correct typing rather than unknown.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping it up
&lt;/h3&gt;

&lt;p&gt;Zustand is an awesome library that is simple to use. I recommend using it in any project you may be working on. If you have any other questions or stories about your migration process leave them in the comments below!&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s connect
&lt;/h3&gt;

&lt;p&gt;If you liked this feel free to connect with me on &lt;a href="https://www.linkedin.com/in/relatablecode" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/relatablecoder" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out my free developer roadmap and weekly tech industry news in my &lt;a href="https://relatablecode.substack.com/" rel="noopener noreferrer"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Dealing with Burnout as a Programmer</title>
      <dc:creator>Diego (Relatable Code)</dc:creator>
      <pubDate>Mon, 26 Sep 2022 23:52:13 +0000</pubDate>
      <link>https://forem.com/diballesteros/dealing-with-burnout-as-a-programmer-5ana</link>
      <guid>https://forem.com/diballesteros/dealing-with-burnout-as-a-programmer-5ana</guid>
      <description>&lt;p&gt;Burnout is a mix of exhaustion, indifference, and annoyance to something you previously loved. This is a feeling that can be quite common in the world of programming. Where you once had joyful curiosity when opening up Visual Studio Code you now feel anxiety or boredom.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y6CI3twV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2AzEcY7qbd6o802jYs" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y6CI3twV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2AzEcY7qbd6o802jYs" alt="anxiety" width="880" height="586"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A more textbook definition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Burn-out is a syndrome conceptualized as resulting from chronic workplace stress that has not been successfully managed.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;World Health Organization&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This can happen for a variety of different reasons but can have long-lasting impacts on how you feel towards your craft and job. I strongly feel that it is something that everyone should be careful with. Take note that this article is not a definitive how-to guide but more of my experience with a burnout in the hopes that others can relate.&lt;/p&gt;

&lt;h3&gt;
  
  
  How it started
&lt;/h3&gt;

&lt;p&gt;I’ve been a developer for several years now. And throughout my entire career, I’ve greatly enjoyed programming and studying outside of work hours. I would do (and throw away) a ton of side projects.&lt;/p&gt;

&lt;p&gt;The majority of the time was a ton of fun! I felt like I was picking up new skills and building neat projects even if they were of no monetary value. This continued for several years without an issue.&lt;/p&gt;

&lt;p&gt;But, on top of it being fun, I also felt the drive to &lt;strong&gt;continue&lt;/strong&gt; this behavior. I wanted to stay on top of my craft. I felt like it was a &lt;strong&gt;necessity&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N3SX9jiN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/612/0%2A44UZ22OfbyNhpq9_" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N3SX9jiN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/612/0%2A44UZ22OfbyNhpq9_" alt="necessity" width="612" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, a few months back I had several real-life issues that were affecting my personal mood, and the free time I had available. Since I had always worked on side projects I didn’t see a need to stop but continued doing them despite all the built-up stress.&lt;/p&gt;

&lt;p&gt;This continued for almost a year.&lt;/p&gt;

&lt;h3&gt;
  
  
  The creeping feeling
&lt;/h3&gt;

&lt;p&gt;In the beginning, this wasn’t an issue. I would continue to work. But, something felt off. I would open up my text editor and work but my joy in doing so would decrease every time.&lt;/p&gt;

&lt;p&gt;Where my attention before was completely undivided in my project, it was now easily diverted into YouTube, video games, or other things. I would feel justified in procrastinating things I had planned out beforehand.&lt;/p&gt;

&lt;p&gt;Pushing it out further and further into the future. “I’ll finish it eventually”&lt;/p&gt;

&lt;p&gt;Even after dealing with my other issues in life, I began to feel stress and anxiety when working on projects or articles outside of work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tCvLeDXq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2AJJ8NN50-de-7RqlK" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tCvLeDXq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2AJJ8NN50-de-7RqlK" alt="burning" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At its peak, I just couldn’t find the desire to work on much of anything.&lt;/p&gt;

&lt;h3&gt;
  
  
  The creeping feeling
&lt;/h3&gt;

&lt;p&gt;I just had to &lt;strong&gt;stop&lt;/strong&gt;. Plain and simple. I could no longer bring myself to code as I felt It was quickly becoming a downward cycle. I would feel anxiety when working on side projects, but feel guilty if I didn’t continue them.&lt;/p&gt;

&lt;p&gt;I took a few months off where I did limited coding outside of work. Slowly over time, I began feeling the desire to code on projects and begin studying. But I did not want to waste this feeling. I began to see this feeling of curiosity and passion to learn as a resource.&lt;/p&gt;

&lt;p&gt;I want to avoid this creeping feeling from now on. I now try to time and intentionally plan the time I dedicate to learning and programming outside of work. I feel like this balanced approach is way healthier.&lt;/p&gt;

&lt;p&gt;This may seem obvious to some, but it required introspection on my part. I realized that even if I love what I do I have to be careful not to overdo it.&lt;/p&gt;

&lt;p&gt;Passion is awesome and should be treated with care.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fgcbSoBI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/612/0%2ABNjq5UF0Ediw8eZF" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fgcbSoBI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/612/0%2ABNjq5UF0Ediw8eZF" alt="sunrise" width="612" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If anyone has experienced burnout let me know in the comments below how you dealt with it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s connect
&lt;/h3&gt;

&lt;p&gt;If you liked this feel free to connect with me on &lt;a href="https://www.linkedin.com/in/relatablecode"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/relatablecoder"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out my free developer roadmap and weekly tech industry news in my &lt;a href="https://relatablecode.substack.com/"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>mentalhealth</category>
      <category>programming</category>
      <category>burnout</category>
    </item>
    <item>
      <title>Testing a React application updating to Cypress 10</title>
      <dc:creator>Diego (Relatable Code)</dc:creator>
      <pubDate>Mon, 13 Jun 2022 14:13:00 +0000</pubDate>
      <link>https://forem.com/diballesteros/testing-a-react-application-updating-to-cypress-10-175d</link>
      <guid>https://forem.com/diballesteros/testing-a-react-application-updating-to-cypress-10-175d</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the last article of this series, we went over how to create a test with Cypress, an end-to-end testing framework. Check it out &lt;a href="https://relatablecode.com/testing-a-react-application-creating-a-cypress-test"&gt;here&lt;/a&gt;. Cypress is a featureful framework that has only improved with its new version, and its main view got a makeover to give it a more modern look.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--12zR2zxr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/958/0%2APEUfR9c2vYhBjH5q" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--12zR2zxr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/958/0%2APEUfR9c2vYhBjH5q" alt="barebones react application" width="880" height="660"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s briefly go over the small barebones application we’ve made up to this point.&lt;/p&gt;

&lt;p&gt;When clicking on a button it fetches posts from a service and displays them on the page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TZgdTjLp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/593/0%2AlzSWKmKdVXkzf582" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TZgdTjLp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/593/0%2AlzSWKmKdVXkzf582" alt="post fetching" width="593" height="675"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For reference, you can find the repository &lt;a href="https://github.com/diballesteros/react-testing"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating Cypress
&lt;/h2&gt;

&lt;p&gt;First, let’s go ahead and update our Cypress package. If this is your first time using Cypress, it’ll also install it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add cypress &lt;span class="nt"&gt;--dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or if you’re using npm first uninstall it then re-install it. I've had issues with updating the package so I prefer reinstalling it fresh. If someone in the comments has a better way please let me know!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm uninstall cypress 
npm &lt;span class="nb"&gt;install &lt;/span&gt;cypress &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should update Cypress to its newest version 10. As of this article, this is version 10.1.0. If you are having compatibility issues while reading this article I suggest checking out the official &lt;a href="https://docs.cypress.io/guides/references/changelog"&gt;Cypress changelog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Easy enough. Now let’s head into the breaking changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cypress Configuration file
&lt;/h2&gt;

&lt;p&gt;Let’s try running Cypress.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run dev 
npm run test-e2e
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you still have cypress open you’ll notice a similar error:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r2jYLj_A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2AfFxKIsqPILeW53eR" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r2jYLj_A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2AfFxKIsqPILeW53eR" alt="cypress error" width="880" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cypress now uses a different configuration file. Let’s delete our cypress.json and create a new cypress.config.js&lt;/p&gt;

&lt;p&gt;This new file has a defineConfig function that will contain all of our old options. Which in this case was just the baseUrl option.&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cypress&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&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="nx"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;e2e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;baseUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:3000&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;specPattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cypress/integration/**/*.spec.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;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we’ll get the new modern screen:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fgqOtrwb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/990/0%2AMegc-r4u5un_o38f" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fgqOtrwb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/990/0%2AMegc-r4u5un_o38f" alt="new cypress screen" width="880" height="567"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Resolving errors
&lt;/h2&gt;

&lt;p&gt;We’ll get met with our first error when we click on the end-to-end testing option:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--p44qmFgI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/943/0%2ARmP-r6cbrO9uG7-1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--p44qmFgI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/943/0%2ARmP-r6cbrO9uG7-1" alt="first cypress error" width="880" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is simple, head into your Cypress directory and rename cypress/support/index.js to cypress/support/e2e.js&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1Wq0yDz9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/971/0%2Anq3Np6zyurrd7Rgo" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1Wq0yDz9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/971/0%2Anq3Np6zyurrd7Rgo" alt="cypress directory" width="880" height="546"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can choose a browser to launch our tests. Let’s start testing in Chrome. When we click on it we’ll see the option to create our first spec. But wait, where did all of our old test files go?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uDMxjKMg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/743/0%2ARajL9t6he7QWO713" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uDMxjKMg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/743/0%2ARajL9t6he7QWO713" alt="spec files missing" width="743" height="580"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;if we click on &lt;strong&gt;View spec pattern&lt;/strong&gt; we’ll see how it’s looking for tests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E_T_vxL1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/635/0%2AoqPZt82ilmlrGYSX" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E_T_vxL1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/635/0%2AoqPZt82ilmlrGYSX" alt="spec pattern" width="635" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looks like Cypress now defaults to the **.cy.{js,jsx,ts,tsx} extensions. Let's just update the spec pattern for the sake of our example project. In our Cypress configuration file let's add a new specPattern property.&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cypress&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&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="nx"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;e2e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;baseUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:3000&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;specPattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cypress/integration/**/*.spec.js&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we’ll see all of our tests again!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GqIxKWlP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/999/0%2Aog73QFs2B7qAGecs" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GqIxKWlP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/999/0%2Aog73QFs2B7qAGecs" alt="seeing tests again" width="880" height="697"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can now run our tests as normal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Some clean-up
&lt;/h3&gt;

&lt;p&gt;We can also delete our plugins folder inside our cypress folder as it is no longer needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping it up
&lt;/h2&gt;

&lt;p&gt;Due to Cypress’ new version, I decided it was best to first update our version. Next time we’ll cover what I had to delay, hooking up our testing frameworks with a code coverage functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s connect
&lt;/h2&gt;

&lt;p&gt;If you liked this feel free to connect with me on &lt;a href="https://www.linkedin.com/in/relatablecode"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/relatablecoder"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out my free developer roadmap and weekly tech industry news in my &lt;a href="https://relatablecode.substack.com/"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Testing a React Application Creating a Cypress Test</title>
      <dc:creator>Diego (Relatable Code)</dc:creator>
      <pubDate>Mon, 06 Jun 2022 14:06:23 +0000</pubDate>
      <link>https://forem.com/diballesteros/testing-a-react-application-creating-a-cypress-test-53dn</link>
      <guid>https://forem.com/diballesteros/testing-a-react-application-creating-a-cypress-test-53dn</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;The seventh part of my ongoing series is on how to test a modern React application. This time I’ll go over how to create our first &lt;a href="https://www.cypress.io/" rel="noopener noreferrer"&gt;Cypress&lt;/a&gt; test.&lt;/p&gt;

&lt;p&gt;Cypress is a robust end-to-end testing framework.&lt;/p&gt;

&lt;p&gt;In the previous parts of this series, I went over how to set up our unit-testing framework with Vitest. Unit tests will cover more isolated and individual pieces like our functions or component-only tests if necessary.&lt;/p&gt;

&lt;p&gt;We also went over &lt;a href="https://relatablecode.com/testing-a-react-application-setting-up-cypress" rel="noopener noreferrer"&gt;how to initially set up Cypress&lt;/a&gt; and its dependencies. Cypress will be handling a big chunk of our tests and give us the most confidence in our tests.&lt;/p&gt;

&lt;p&gt;Let’s briefly go over the small barebones application we’ve made up to this point.&lt;/p&gt;

&lt;p&gt;When clicking on a button it fetches posts from a service and displays them on the page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F593%2F0%2AM73tUC2gO_rU5Ad8" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F593%2F0%2AM73tUC2gO_rU5Ad8" alt="example applciation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For reference, you can find the repository &lt;a href="https://github.com/diballesteros/react-testing" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cypress examples
&lt;/h3&gt;

&lt;p&gt;Let’s go ahead and run the commands needed to get Cypress up and running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run dev 
npm run test-e2e
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When first launching the application you’ll see when it completes through the terminal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F312%2F0%2A-6XZq4i864wjoFYy" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F312%2F0%2A-6XZq4i864wjoFYy" alt="terminal results"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A window will be opened with some tests that we can review with a breadth of examples.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F984%2F0%2Ag8vL26K7Qop3YG7e" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F984%2F0%2Ag8vL26K7Qop3YG7e" alt="cypress window"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I highly recommend checking out these tests to get a real example of how to use a lot of the syntax with Cypress. They cover quite a bit of use cases. These tests can be found in the following location in our folder structure:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F272%2F0%2Az0PxYjzGR-JPQzHm" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F272%2F0%2Az0PxYjzGR-JPQzHm" alt="folder structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating our first test
&lt;/h3&gt;

&lt;p&gt;Let’s go ahead and start creating our first test. In the integrations folder let's create a file &lt;strong&gt;first-test.spec.js&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Cypress syntax is very similar to our vitest syntax. We'll start off by adding an encapsulating describe function.&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="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;First Test&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the second parameter of the function, we’ll add our individual test. Again, similar to vitest we'll add an it function with our first test logic.&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="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;First Test&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;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fetch posts&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s pause and take a look again at the Cypress window. At the bottom of the list, we’ll find the newly added test.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F182%2F0%2AETr_M3j_5iHClCic" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F182%2F0%2AETr_M3j_5iHClCic" alt="newly added test"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we click on the test we’ll get a message that no tests are found. Let’s go ahead and start using the Cypress syntax to navigate to the page.&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="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;First Test&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;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fetch posts&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="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:3000/&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just to break down what’s going on, cy is a globally available object with a bunch of our Cypress syntax. cy.visit() allows us to visit the specified URL. In the Cypress window, we'll get our page that is up from our dev environment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2AhsCOmFZ7OHR0AenW" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2AhsCOmFZ7OHR0AenW" alt="page in dev window"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the center right next to the URL we’ll see a cross-hair icon. If you click on this icon you’ll be able to select other elements on the page. What is this for?&lt;/p&gt;

&lt;p&gt;You can select elements to query for in our Cypress tests. Awesome 😎. This is one of the reasons Cypress is so fun to use. It really makes creating tests a breeze. We could just use the existing selector for the fetch button but let’s add a different one to our JSX.&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;testid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fetch-posts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;fetchPosts&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;Fetch&lt;/span&gt; &lt;span class="nx"&gt;Posts&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The attribute data-testid will allow us to query for the button with higher specificity. If we go ahead and use the crosshair icon in our Cypress window we can see the new selector.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F460%2F0%2Avsferie5T8oYxDiD" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F460%2F0%2Avsferie5T8oYxDiD" alt="crosshair icon"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clicking the copy button on the right we can grab the selector and use it in our test.&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="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;First Test&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;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fetch posts&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="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:3000/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;cy&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-testid="fetch-posts"]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;click&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;In our testing window, we’ll see the posts correctly fetched. As the final part of our first test let’s go ahead and assert that the posts were fetched correctly. But our fetch of the posts is asynchronous. No worries here as Cypress has us covered.&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="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;First Test&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;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fetch posts&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="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[http://localhost:3000/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="na"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//localhost:3000/'));&lt;/span&gt;
        &lt;span class="nx"&gt;cy&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-testid="fetch-posts"]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;intercept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[https://jsonplaceholder.typicode.com/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="p"&gt;(](&lt;/span&gt;&lt;span class="na"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//jsonplaceholder.typicode.com/posts').as()&lt;/span&gt;
            &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getPosts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[@getPosts](http://twitter.com/getPosts)&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our cy object has an intercept property that allows us to check network requests for a specific endpoint. We alias this request by chaining an as function with the name of the alias. At the very end, we'll wait for the request with the cy.wait('@getPosts') command.&lt;/p&gt;

&lt;p&gt;The only thing left to do is check that the posts are correctly loaded. Let’s add another data-testid to the list of our posts.&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;section&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;testid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;posts&lt;/span&gt;&lt;span class="dl"&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;posts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;post&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;article&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h3&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;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h3&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&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;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/article&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;))}&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/section&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that out of the way, we can query for the number of posts.&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="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;First Test&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;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fetch posts&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="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:3000/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;intercept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getPosts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;cy&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-testid="fetch-posts"]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@getPosts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;cy&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-testid="posts"]&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="nf"&gt;children&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;should&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;have.length.greaterThan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&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;And we’re done, in the Cypress window, you can see the test passing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping it up
&lt;/h3&gt;

&lt;p&gt;I hope you enjoyed making our first Cypress test. This framework will give you a ton of coverage of the entire application. Speaking of coverage, in the next article I’ll go over how to hook up vitest and Cypress to a code coverage system.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s connect
&lt;/h3&gt;

&lt;p&gt;If you liked this feel free to connect with me on &lt;a href="https://www.linkedin.com/in/relatablecode" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/relatablecoder" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out my free developer roadmap and weekly tech industry news in my &lt;a href="https://relatablecode.substack.com/" rel="noopener noreferrer"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>coding</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript LeetCode Valid Anagram</title>
      <dc:creator>Diego (Relatable Code)</dc:creator>
      <pubDate>Mon, 30 May 2022 21:33:57 +0000</pubDate>
      <link>https://forem.com/diballesteros/javascript-leetcode-valid-anagram-2001</link>
      <guid>https://forem.com/diballesteros/javascript-leetcode-valid-anagram-2001</guid>
      <description>&lt;h4&gt;
  
  
  Prompt
&lt;/h4&gt;

&lt;p&gt;An &lt;strong&gt;Anagram&lt;/strong&gt; is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: s = "anagram", t = "nagaram" 
Output: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Analyzing the problem
&lt;/h3&gt;

&lt;p&gt;Let’s this of what the problem is asking. We have to know the contents of the strings we’re analyzing. We also have to make sure that we’re keeping track of the letters as they should be exactly the same to confirm that it is an  &lt;strong&gt;anagram&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Following that train of thought, we can early return if there is ever a difference in the letters they contain. But what does it mean to have the same letters? Well, essentially it’s mapping out the frequency of each letter for the first word we iterate over. When we iterate over the second word we can double-check with what.&lt;/p&gt;

&lt;p&gt;Checking back on the first example provided we can visualize this by counting each letter:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--doylWqPK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/285/0%2AChfe42aFbcNsbWgg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--doylWqPK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/285/0%2AChfe42aFbcNsbWgg" alt="javascript leetcode" width="285" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looking at the counts we can use this to validate in place as we iterate over the second word. Let’s see how this would look:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xyb5KocZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/265/0%2AA9HZ5IFUyCpotPHS" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xyb5KocZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/265/0%2AA9HZ5IFUyCpotPHS" alt="javascript leetcode" width="265" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the first time we run into the n in the second iteration we subtract it from the count. Anytime we go below 0 then we know the words are &lt;strong&gt;not&lt;/strong&gt; valid anagrams.&lt;/p&gt;

&lt;p&gt;Cool 😎, let’s get to coding.&lt;/p&gt;

&lt;h3&gt;
  
  
  The solution
&lt;/h3&gt;

&lt;p&gt;Similar to our &lt;a href="https://relatablecode.com/javascript-leetcode-best-time-to-buy-and-sell-stock"&gt;last problem&lt;/a&gt; we can use a hash table.&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;isAnagram&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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;hashTable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&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;j&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]]){&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
        &lt;span class="p"&gt;}&lt;/span&gt; 
        &lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&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="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&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;In the first iteration, we add the frequency of every letter. If it doesn’t exist we create a 0 value at that point.&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the second iteration, we subtract all values. If no letter ever exists then we’ll hit the number 0 which means we'll return a false.&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]]){&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Let’s connect
&lt;/h3&gt;

&lt;p&gt;If you liked this feel free to connect with me on &lt;a href="https://www.linkedin.com/in/relatablecode"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/relatablecoder"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out my free developer roadmap and weekly tech industry news in my &lt;a href="https://relatablecode.substack.com/"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>javascript</category>
      <category>datastructures</category>
      <category>programming</category>
    </item>
    <item>
      <title>Testing a React Application Setting up Cypress</title>
      <dc:creator>Diego (Relatable Code)</dc:creator>
      <pubDate>Mon, 23 May 2022 14:36:56 +0000</pubDate>
      <link>https://forem.com/diballesteros/testing-a-react-application-setting-up-cypress-i1d</link>
      <guid>https://forem.com/diballesteros/testing-a-react-application-setting-up-cypress-i1d</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;The sixth part of my ongoing series is on how to test a modern React application. This time I’ll go over how to initially configure &lt;a href="https://www.cypress.io/"&gt;Cypress&lt;/a&gt;, our End-to-End testing framework.&lt;/p&gt;

&lt;p&gt;In the previous parts of this series, I went over how to set up our &lt;a href="https://relatablecode.com/testing-a-react-application-integrating-msw-with-vitest"&gt;unit-testing framework with Vitest&lt;/a&gt;. Unit tests will cover more isolated and individual pieces of code like our functions or component-only tests if necessary.&lt;/p&gt;

&lt;p&gt;On the other hand, Cypress and its test will handle the other big chunk where we can really gain confidence that our tests will behave similarly to how a user would use the application. Just to catch up let’s go over what our simple barebones app does.&lt;/p&gt;

&lt;p&gt;When clicking on a button it fetches posts from a service and displays them on the page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P6Ns9Mlh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/593/0%2AFWYJqQ0h_j3hyFFb" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P6Ns9Mlh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/593/0%2AFWYJqQ0h_j3hyFFb" alt="Relatable Code Modern React Testing" width="593" height="675"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Cypress?
&lt;/h3&gt;

&lt;p&gt;The first phrase you’ll see when navigating to their page is the following:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Fast, easy and reliable testing for anything that runs in a browser.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The keyword for this quote is &lt;strong&gt;browser&lt;/strong&gt;. Cypress’ test runner replicates the browser environment. That means whatever test we create will behave exactly as if running in the browser. This gives us a ton of confidence!&lt;/p&gt;

&lt;p&gt;On top of that, with Cypress we can test with several different browser environments: Google Chrome and Firefox being among them. Cypress comes with its own syntax at times but if you’re familiar with Vitest or Jest then it should come naturally. The docs are actively maintained and well structured.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.cypress.io/"&gt;Take a look at them&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eSxlSLaR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/687/0%2ANslJsPuKorN1OZMb" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eSxlSLaR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/687/0%2ANslJsPuKorN1OZMb" alt="thumbs up" width="687" height="1031"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Photo by Sincerely Media on Unsplash&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Enough talking, let’s start setting up Cypress.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuration Files for Cypress
&lt;/h3&gt;

&lt;p&gt;First, we have to install the necessary dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;cypress &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add cypress &lt;span class="nt"&gt;--dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Much like other libraries, this will have a configuration file at the root of our project: cypress.json. Let's go ahead and create it. Within this file, you can set a variety of different flags. Check out this &lt;a href="https://docs.cypress.io/guides/references/configuration#Global"&gt;article&lt;/a&gt; in the docs to better understand all the options.&lt;/p&gt;

&lt;p&gt;When first launching any Cypress test we have to manually visit the pages we want to test. So instead of writing out the entire URL every single time we’ll go ahead and include the baseUrl in this config.&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;"baseUrl"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"http://localhost:3000"&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;With that, the only other configuration we need to set up is the scripts in our package.json.&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="nl"&gt;"scripts"&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="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;...other&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;scripts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"e2e"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"cypress open"&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="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check out the full repository up to this point &lt;a href="https://github.com/diballesteros/react-testing"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Next time we’ll create our first test with Cypress as well as make sure our integration with MSW holds up.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s connect
&lt;/h3&gt;

&lt;p&gt;If you liked this feel free to connect with me on &lt;a href="https://www.linkedin.com/in/relatablecode"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/relatablecoder"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out my free developer roadmap and weekly tech industry news in my &lt;a href="https://relatablecode.substack.com/"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>testing</category>
      <category>cypress</category>
      <category>javascript</category>
    </item>
    <item>
      <title>JavaScript LeetCode Best Time to Buy and Sell Stock</title>
      <dc:creator>Diego (Relatable Code)</dc:creator>
      <pubDate>Mon, 16 May 2022 13:15:11 +0000</pubDate>
      <link>https://forem.com/diballesteros/javascript-leetcode-best-time-to-buy-and-sell-stock-205l</link>
      <guid>https://forem.com/diballesteros/javascript-leetcode-best-time-to-buy-and-sell-stock-205l</guid>
      <description>&lt;h3&gt;
  
  
  Prompt
&lt;/h3&gt;

&lt;p&gt;You are given an array prices where prices[i] is the price of a given stock on the ith day.&lt;/p&gt;

&lt;p&gt;You want to maximize your profit by choosing a &lt;strong&gt;single day&lt;/strong&gt; to buy one stock and choosing a &lt;strong&gt;different day in the future&lt;/strong&gt; to sell that stock.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the maximum profit you can achieve from this transaction&lt;/em&gt;. If you cannot achieve any profit, return 0.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Input: prices = [7,1,5,3,6,4] 
Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Analyzing the problem
&lt;/h3&gt;

&lt;p&gt;As with the previous problems the first apparent solution is to go for the brute force solution. Here we would iterate over the array twice checking every subtraction between every subsequent value and just grab the highest profit.&lt;/p&gt;

&lt;p&gt;A visual example of this could look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F432%2F0%2AeBt9yg14Iqcj4DqD" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F432%2F0%2AeBt9yg14Iqcj4DqD" alt="JavaScript Leetcode best time to buy and sell stock"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  First solution
&lt;/h3&gt;

&lt;p&gt;Let’s go ahead and write out the code for what we mentioned above.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;maxProfit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;profit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&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;currentProfit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentProfit&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;profit&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;profit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentProfit&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;return&lt;/span&gt; &lt;span class="nx"&gt;profit&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;Here we compare the price of the next element in the array with the current. The best way to conceptualize this is that i is our index and j is our pointer that keeps iterating.&lt;/p&gt;

&lt;p&gt;We use two for loops to find the maximum profit. This will have a complexity of O(n^2). This is a pretty big problem as it is very slow! However, we can optimize this by iterating over the array only once.&lt;/p&gt;

&lt;h3&gt;
  
  
  Second Solution
&lt;/h3&gt;

&lt;p&gt;But what would a single iteration look like?&lt;/p&gt;

&lt;p&gt;With code it would look something like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;maxProfit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;profit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;stockToBuy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prices&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="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stockToBuy&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;stockToBuy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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;currentProfit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;stockToBuy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentProfit&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;profit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;profit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentProfit&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;return&lt;/span&gt; &lt;span class="nx"&gt;profit&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;We grab the initial stockToBuy from the first element in the array. Then we can begin iterating over the array (skipping the first). We compare if the next stock is of a higher price than our currently selected stock. If so it'll yield us a higher profit so we switch it out.&lt;/p&gt;

&lt;p&gt;However, this also serves as a reset mechanism. If the stock is of a higher value we know that from this point forward it will not be higher profit than if it was with the previously selected stock.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;stockToBuy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After that, we calculate our new currentProfit, this is just a temporary value in our current iteration.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;currentProfit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;stockToBuy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then we compare it with the value we saved outside of our for loop. If it's higher then that's exactly what we want.&lt;/p&gt;

&lt;p&gt;Notice we created the variable with 0 at first so that if we don't find any profit we can just return it as is.&lt;/p&gt;

&lt;h4&gt;
  
  
  Greedy Algorithm
&lt;/h4&gt;

&lt;p&gt;But what is this called?&lt;/p&gt;

&lt;p&gt;What we’re doing here is selecting the best option on every iteration and forgetting about past selections This is called a greedy algorithm.&lt;/p&gt;

&lt;p&gt;Here’s a &lt;a href="https://en.wikipedia.org/wiki/Greedy_algorithm" rel="noopener noreferrer"&gt;Wikipedia article&lt;/a&gt; if you want to read up more on it (you should).&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s connect
&lt;/h3&gt;

&lt;p&gt;If you liked this feel free to connect with me on &lt;a href="https://www.linkedin.com/in/relatablecode" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/relatablecoder" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out my free developer roadmap and weekly tech industry news in my &lt;a href="https://relatablecode.substack.com/" rel="noopener noreferrer"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>leetcode</category>
      <category>javascript</category>
      <category>coding</category>
    </item>
    <item>
      <title>JavaScript LeetCode Contains Duplicate</title>
      <dc:creator>Diego (Relatable Code)</dc:creator>
      <pubDate>Thu, 12 May 2022 16:32:49 +0000</pubDate>
      <link>https://forem.com/diballesteros/javascript-leetcode-contains-duplicate-5c9k</link>
      <guid>https://forem.com/diballesteros/javascript-leetcode-contains-duplicate-5c9k</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Continuing through the problems of LeetCode. I am not haphazardly selecting any questions. I am following along with this list for those that were curious:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.techinterviewhandbook.org/best-practice-questions"&gt;techinterviewhandbook.org/best-practice-que..&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Prompt
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Given an integer array&lt;/em&gt; &lt;em&gt;nums , return&lt;/em&gt; &lt;em&gt;true if any value appears&lt;/em&gt; &lt;strong&gt;&lt;em&gt;at least twice&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;in the array, and return&lt;/em&gt; &lt;em&gt;false if every element is distinct.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [1,2,3,1] 
Output: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, the problem seems to be pretty simple. The idea here is to iterate over the array and find any duplicates. There’s a bit of a cheat code we can use with JavaScript in this one.&lt;/p&gt;

&lt;h3&gt;
  
  
  First Solution (cheating)
&lt;/h3&gt;

&lt;p&gt;There is a standard built-in object for JavaScript called &lt;a href="%5Bdeveloper.mozilla.org/en-US/docs/Web/JavaSc..%5D(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)"&gt;Set&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But what does this object do?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Set objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in the&lt;/em&gt; &lt;em&gt;Set's collection.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Knowing this, the only thing we need to do is convert our array into a Set and compare its length with the original number array length.&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;containsDuplicate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&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="kd"&gt;set&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;([...&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;Converting an array into a Set is simple, just spread it into a new array in the Set constructor. It has a property to calculate the size (number of items). We just have to compare that with the length of the original array, nums.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return set.size != nums.length;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Second Solution
&lt;/h3&gt;

&lt;p&gt;Much like our (&lt;a href="https://relatablecode.com/javascript-leetcode-two-sum"&gt;first solution&lt;/a&gt;) we can create a hash table of our array as we’re iterating and evaluate it in place.&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;containsDuplicate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&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;hashTable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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;To break this down a little bit: we first iterate over the array of numbers. If the map already has the value then we return true. We check this with the .has property of map s&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hashtable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Otherwise, we add it to the map and move on.&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="k"&gt;else&lt;/span&gt; &lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value is pretty irrelevant as we don’t really care much for it. There probably is a data structure more suited here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s connect
&lt;/h3&gt;

&lt;p&gt;More content at &lt;a href="https://relatablecode.com"&gt;Relatable Code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you liked this feel free to connect with me on &lt;a href="https://www.linkedin.com/in/relatablecode"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/relatablecoder"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out my free developer roadmap and weekly tech industry news in my &lt;a href="https://relatablecode.substack.com/"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>programming</category>
      <category>datastructures</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>JavaScript LeetCode Two Sum</title>
      <dc:creator>Diego (Relatable Code)</dc:creator>
      <pubDate>Mon, 09 May 2022 13:17:32 +0000</pubDate>
      <link>https://forem.com/diballesteros/javascript-leetcode-two-sum-5hkp</link>
      <guid>https://forem.com/diballesteros/javascript-leetcode-two-sum-5hkp</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;LeetCode. An unfortunate truth of developer life is that for some job interviews it is necessary to learn Data structure and algorithms (DSA) in a specific way. A way in which you’re expected to code up a solution to a DSA problem where you would otherwise google it.&lt;/p&gt;

&lt;p&gt;I say &lt;strong&gt;unfortunate&lt;/strong&gt; because in most cases it is unnecessary to know them to the degree required for the technical interviews and hardly reflects actual job capability.&lt;/p&gt;

&lt;p&gt;But I digress, LeetCode is a website that compiles tons of different DSA problems. I’ll be going through several LeetCode problems over the next month, explaining and solving them, to help others out. And also it helps me to re-write the solution. 😎&lt;/p&gt;

&lt;h3&gt;
  
  
  Prompt
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Given an array of integers&lt;/em&gt; &lt;em&gt;nums and an integer&lt;/em&gt; &lt;em&gt;target, return&lt;/em&gt; indices of the two numbers such that they add up to target_._&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You may assume that each input would have&lt;/em&gt; &lt;strong&gt;exactly &lt;em&gt;one solution&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;, and you may not use the&lt;/em&gt; same &lt;em&gt;element twice.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You can return the answer in any order.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Example 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [2,7,11,15], target = 9 
Output: [0,1] 
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Thinking it through
&lt;/h3&gt;

&lt;p&gt;This is the first LeetCode problem and is quite an easy one. What first comes to mind is just iterating over the array twice and finding the necessary sum. This will 100% give us the solution.&lt;/p&gt;

&lt;p&gt;However, as many of you may know about these types of questions. Usually, it is not enough to be correct. For DSA problems you want to be as &lt;strong&gt;efficient&lt;/strong&gt; as possible.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F640%2F0%2Aesfo1ShHyjQTDEsv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F640%2F0%2Aesfo1ShHyjQTDEsv.jpg" alt="clock"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Photo by Aron Visuals on Unsplash&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So for example our first solution would be something along these lines:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F212%2F0%2Aoo0J5WkUpeVByk4X.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F212%2F0%2Aoo0J5WkUpeVByk4X.png" alt="first solution"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Where i is the value of our first iteration and j is the value of our second iteration. Once added up we get 9 which is the solution we need.&lt;/p&gt;

&lt;h3&gt;
  
  
  First solution
&lt;/h3&gt;

&lt;p&gt;I mainly use JavaScript so the solution would look something like this:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nf"&gt;twoSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
     &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&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;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&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;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
             &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&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="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;j&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;nums is the array of numbers and target is the desired solution. I iterate over both arrays and check if the sum is the necessary value. However, as stated in the prompt, we should not use the same element twice.&lt;/p&gt;

&lt;p&gt;So we can check if the iterations are on the same element and skip:&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, as I mentioned before this is very unoptimized.&lt;/p&gt;

&lt;h3&gt;
  
  
  Second Solution
&lt;/h3&gt;

&lt;p&gt;A much better solution would be searching for the target based on the &lt;strong&gt;current&lt;/strong&gt; number. What does that mean?&lt;/p&gt;

&lt;p&gt;For example, when you first start iterating over the array you know the exact number you’re looking for. The number you’re looking for is the &lt;strong&gt;result of the subtraction of the target and the current iterated value.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s take our first array as an example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F205%2F0%2AtIKHBKcR3tr4ZqdB" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F205%2F0%2AtIKHBKcR3tr4ZqdB" alt="first array example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we’re on the first element 2 we know that we need to find as 9 minus 2 is 7. But, we need a way to quickly re-access data of the array.&lt;/p&gt;

&lt;p&gt;Introducing a Hash Table.&lt;/p&gt;

&lt;h4&gt;
  
  
  Hash Table
&lt;/h4&gt;

&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Hash_table#:~:text=In%20computing%2C%20a%20hash%20table,desired%20value%20can%20be%20found." rel="noopener noreferrer"&gt;hash table&lt;/a&gt; is a data structure that maps keys to values. This is perfect for our use case because we just want to map our number to our index.&lt;/p&gt;

&lt;p&gt;But how do we create this hash table?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" rel="noopener noreferrer"&gt;Map&lt;/a&gt; is a Javascript built-in object. The only thing you need to know is that it holds key-value pairs and has an API to quickly check the content:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.set&lt;/code&gt; for setting the key and value pair&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.get&lt;/code&gt; for getting the value based on the key&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.has&lt;/code&gt; for checking if it has the key&lt;/p&gt;

&lt;h3&gt;
  
  
  Second Solution
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hashTable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 

&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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;substractionWeWant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;substractionGoal&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="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hashTable&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="nx"&gt;substractionGoal&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;hashTable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&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;We now have a simplified way to check for previous values. Going back to the code we first do a single iteration of the loop. By subtracting the target value from the current index value num we can check if the remainder already exists. If it doesn't we just add the value we just tried to the map and move on.&lt;/p&gt;

&lt;p&gt;So to reiterate&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We loop over the array&lt;/li&gt;
&lt;li&gt;We check if our current map of values has the subtraction of our target and current value we are iterating over. If it does we return both the index of the current value and the index of the value in our map&lt;/li&gt;
&lt;li&gt;If we don’t find a match we add it to our map and move on&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;First solution:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F654%2F0%2AUdI3G3Wyt67b9fwE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F654%2F0%2AUdI3G3Wyt67b9fwE.png" alt="first solution time"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Second solution:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F660%2F0%2Afxy4RceUcBlcSmS9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F660%2F0%2Afxy4RceUcBlcSmS9.png" alt="second solution time"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping it up
&lt;/h3&gt;

&lt;p&gt;If you want to keep up with this series feel free to follow me on here or any of my social media below!&lt;/p&gt;

&lt;p&gt;More content at &lt;a href="https://relatablecode.com" rel="noopener noreferrer"&gt;Relatable Code&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s connect
&lt;/h3&gt;

&lt;p&gt;If you liked this feel free to connect with me on &lt;a href="https://www.linkedin.com/in/relatablecode" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/relatablecoder" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out my free developer roadmap and weekly tech industry news in my &lt;a href="https://relatablecode.substack.com/" rel="noopener noreferrer"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>coding</category>
      <category>datastructures</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
