<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dave</title>
    <description>The latest articles on DEV Community by Dave (@msoup).</description>
    <link>https://dev.to/msoup</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%2F839063%2F10b5b756-4f4e-4200-84a0-ea2f6b39d81b.jpeg</url>
      <title>DEV Community: Dave</title>
      <link>https://dev.to/msoup</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/msoup"/>
    <language>en</language>
    <item>
      <title>An Exercise with Debugging and Promises</title>
      <dc:creator>Dave</dc:creator>
      <pubDate>Thu, 28 Jul 2022 01:19:00 +0000</pubDate>
      <link>https://dev.to/msoup/an-exercise-with-debugging-and-promises-2m0p</link>
      <guid>https://dev.to/msoup/an-exercise-with-debugging-and-promises-2m0p</guid>
      <description>&lt;p&gt;Debugging skills are one of the hardest to gain as someone new to development, so I thought I'd take the time to share some of the exercises that stumped me. Give this one a try!&lt;/p&gt;

&lt;p&gt;You have two functions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;getResult returns a Promise with a 50% chance to succeed and a 50% chance to reject.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;testGetResult calls getResult and logs the output once. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is the implementation that was given to you:&lt;/p&gt;

&lt;h3&gt;
  
  
  getResult
&lt;/h3&gt;

&lt;p&gt;getResult is an async function that returns a Promise. The Promise will either reject or resolve in 1000 miliseconds, and whether it rejects or resolves depends on a variable generated. The variable will have a value between -0.5 to 0.5; in other words, there is a 50% chance of the variable being positive. If the variable is negative, it rejects. Simple, right?&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="nx"&gt;getResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;0.5&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;number&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;failure&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="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;success&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="mi"&gt;1000&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;getTestResult is even more simple.&lt;br&gt;
It initiates a variable called &lt;code&gt;result&lt;/code&gt; and simply gets the Promise from &lt;code&gt;getResult()&lt;/code&gt;. It will then log whether it succeeded or not.&lt;/p&gt;
&lt;h3&gt;
  
  
  getTestResult
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;testGetResult&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;getResult&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

  &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;h3&gt;
  
  
  The Issue
&lt;/h3&gt;

&lt;p&gt;When running this example, node throws an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "failure".] {
  code: 'ERR_UNHANDLED_REJECTION'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What went wrong?&lt;/p&gt;

&lt;p&gt;I'll update the post with an answer in a few days :)&lt;/p&gt;

&lt;p&gt;Update 08/01&lt;/p&gt;

&lt;p&gt;Hint: what is the type being returned from &lt;code&gt;getResult()&lt;/code&gt;?&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Python Debugging Exercises</title>
      <dc:creator>Dave</dc:creator>
      <pubDate>Sun, 01 May 2022 13:00:27 +0000</pubDate>
      <link>https://dev.to/msoup/python-debugging-exercises-145h</link>
      <guid>https://dev.to/msoup/python-debugging-exercises-145h</guid>
      <description>&lt;p&gt;[The following is a series of debugging exercises that you may encounter in the real world of development. They are based on problems I have been asked. I thought it would be fun to share them.]&lt;/p&gt;

&lt;p&gt;It is your first week at your new job as a software developer. You have your environment set up and you're seated beside a fellow junior who started a few days before you.&lt;/p&gt;

&lt;p&gt;He turns to you and asks if you have a minute.&lt;/p&gt;

&lt;p&gt;"Hey if you don't mind, please help me find out what's wrong..."&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 1
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Context
&lt;/h3&gt;

&lt;p&gt;To change display name of Model Instances, we will use &lt;code&gt;def __str__()&lt;/code&gt; function in a model. The str function in a django model returns a string that is exactly rendered as the display name of instances for that model.&lt;/p&gt;

&lt;p&gt;For example, if we adjust a model in models.py like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.db.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Model&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SomeModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we access SomeModel.objects.all(), the output should be a list of the name fields of this object.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;It doesn't work for comments. When a Comment object is printed, it shows [Comment object (1)] rather than what is defined under str. &lt;/p&gt;

&lt;p&gt;Let's check the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# imports here
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Listing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;max_bid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DecimalField&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AbstractUser&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Comment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;comment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TextField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="n"&gt;listing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ForeignKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Listing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;on_delete&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CASCADE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ForeignKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;on_delete&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CASCADE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;comment&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where did your fellow teammate go wrong?&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 2
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Context
&lt;/h3&gt;

&lt;p&gt;Your coworker was secretly interviewing with other companies and was told to find the min value in a list without using min. &lt;/p&gt;

&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;The below code doesn't seem to find the minimum element in the list. Why?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;minimum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;some_list&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;some_list&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;some_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;some_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where did your fellow teammate go wrong?&lt;/p&gt;

</description>
    </item>
    <item>
      <title>npm ERR! @heroku/buildpack-registry not accessible from @heroku-cli/plugin-buildpacks</title>
      <dc:creator>Dave</dc:creator>
      <pubDate>Sun, 17 Apr 2022 00:04:30 +0000</pubDate>
      <link>https://dev.to/msoup/npm-err-herokubuildpack-registry-not-accessible-from-heroku-cliplugin-buildpacks-4dn6</link>
      <guid>https://dev.to/msoup/npm-err-herokubuildpack-registry-not-accessible-from-heroku-cliplugin-buildpacks-4dn6</guid>
      <description>&lt;p&gt;When trying to push a build over to heroku, you may have encountered the error below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----&amp;gt; Building on the Heroku-20 stack
remote: -----&amp;gt; Using buildpack: heroku/nodejs
remote: -----&amp;gt; Node.js app detected
remote:        
remote: -----&amp;gt; Creating runtime environment
remote:
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NODE_VERBOSE=false
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote:
remote: -----&amp;gt; Installing binaries
remote:        engines.node (package.json):  14.17.1
remote:        engines.npm (package.json):   unspecified (use default)
remote:
remote:        Resolving node version 14.17.1...
remote:        Downloading and installing node 14.17.1...       
remote:        Using default npm version: 6.14.13
remote:        
remote: -----&amp;gt; Installing dependencies
remote:        Installing node modules
remote:        npm ERR! @heroku/buildpack-registry not accessible from @heroku-cli/plugin-buildpacks
remote:
remote:        npm ERR! A complete log of this run can be found 
in:
remote:        npm ERR!     /tmp/npmcache.ipzPa/_logs/2022-04-16T23_28_03_880Z-debug.log
remote: 
remote: -----&amp;gt; Build failed
remote:        
remote:        We're sorry this build is failing! You can troubleshoot common issues here:
remote:        https://devcenter.heroku.com/articles/troubleshooting-node-deploys
remote:
remote:        If you're stuck, please submit a ticket so we can help:
remote:        https://help.heroku.com/
remote:
remote:        Love,
remote:        Heroku
remote:
remote:  !     Push rejected, failed to compile Node.js app.    
remote: 
remote:  !     Push failed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And going to the &lt;a href="https://devcenter.heroku.com/articles/troubleshooting-node-deploys"&gt;specified link above&lt;/a&gt; doesn't seem to address the failure.&lt;/p&gt;

&lt;p&gt;Edit: You may also get the below error for the same issue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm ERR! valid-url not accessible 
from @heroku-cli/plugin-buildpacks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  The Issue
&lt;/h1&gt;

&lt;p&gt;The problem for me was that heroku had no idea which engine to use to install dependencies, so it errored on the dependencies installation of the build. However, the same error will occur if you happen to not have buildpack-registry, so I will lay out some of the possible solutions to this error.&lt;/p&gt;

&lt;h1&gt;
  
  
  Solutions
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Install the buildpacks dependency
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/64629076/heroku-heroku-buildpack-registry-not-accessible-from-heroku-cli-plugin-buil"&gt;Source&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The buildpack-registry package may not be installed. Try installing it!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i @heroku/buildpack-registry&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Install buildpack-registry from the plugins side
&lt;/h2&gt;

&lt;p&gt;It's possible that you may not have buildpacks from the plugins side. Run the below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ heroku plugins:install buildpack-registry
$ heroku plugins:install buildpacks  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/69456585/error-cannot-find-module-heroku-buildpack-registry"&gt;Source&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Make sure your engines are both properly defined in package.json
&lt;/h2&gt;

&lt;p&gt;In my case, my issue was that I did not have the proper engines defined under package.json. My package.json looked 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;{
  "name": "someApp",
  "version": "0.0.0",
  "private": true,
  "engines": {
    "node": "14.17.1",
  },
  "scripts": {
    "start": "node ./bin/www",
    "devstart": "nodemon ./bin/www",
    "heroku-postbuild": "npm run build"

  },
  "dependencies": {
  ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice something?&lt;/p&gt;

&lt;p&gt;In fact, if you look at the original log, you'll see that heroku already gave me a hint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;remote: -----&amp;gt; Installing binaries
remote:        engines.node (package.json):  14.17.1
remote:        engines.npm (package.json):   unspecified (use default)
remote:
remote:        Resolving node version 14.17.1...
remote:        Downloading and installing node 14.17.1...       
remote:        Using default npm version: 6.14.13
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A quick check at my npm version shows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm --version
8.1.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;heroku is using a different npm than what I am using! This is a big no-no in the dev world. &lt;/p&gt;

&lt;p&gt;Adding this in my package.json solved it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "engines": {
    "node": "14.17.1",
    "npm": "8.1.1"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Make sure buildpacks is installed globally
&lt;/h2&gt;

&lt;p&gt;If buildpacks is installed locally, I recommend you remove it and install it globally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm remove @heroku-cli/plugin-buildpacks
npm i -g @heroku-cli/plugin-buildpacks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, remove your node-modules folder and reinstall them with npm i and try to push to heroku again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add a post build script
&lt;/h2&gt;

&lt;p&gt;Under the scripts section of your package.json, add&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"heroku-postbuild": "npm run build"&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Reminders
&lt;/h3&gt;

&lt;p&gt;Don't forget to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;commit your changes to heroku before running &lt;code&gt;git push heroku *branch*&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;check out the error logs to find out exactly what part heroku is failing at&lt;/li&gt;
&lt;li&gt;google for the error message (hopefully by this point you have solved it)&lt;/li&gt;
&lt;li&gt;make sure you overwrite the existing cache, so push with
&lt;code&gt;git push -f heroku master&lt;/code&gt; for your first push until it succeeds. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope I was able to help someone with this. Happy coding.&lt;/p&gt;

</description>
      <category>heroku</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Every Step Mattered: My journey from English teacher to Software Developer.</title>
      <dc:creator>Dave</dc:creator>
      <pubDate>Sat, 09 Apr 2022 11:52:45 +0000</pubDate>
      <link>https://dev.to/msoup/every-step-mattered-my-journey-from-english-teacher-to-software-developer-1ooe</link>
      <guid>https://dev.to/msoup/every-step-mattered-my-journey-from-english-teacher-to-software-developer-1ooe</guid>
      <description>&lt;p&gt;I graduated in 2013 with a Bachelor of Arts in Economics. I came straight to Japan to teach English, abandoning everything I knew to restart my life, a common story you may hear from people who have lived in Japan for a while. &lt;/p&gt;

&lt;p&gt;A lot of people have asked me how I eventually made the switch from English teacher to Software Developer, and I had repeated my story so many times that I finally have the confidence to write about it. I’m not a very good writer, so forgive me for not being able to tell a good story. Luckily, I’m fairly logical, so I can tell a coherent story. I think. I hope?&lt;/p&gt;

&lt;h2&gt;
  
  
  My Timeline
&lt;/h2&gt;

&lt;p&gt;As of this writing, I have been in Japan for 9 years. If I were to summarize my time here (relevant to getting to where I am now) as concisely as possible, it would look like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Timeline (Years)&lt;/th&gt;
&lt;th&gt;Milestones&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Began JET Programme, teaching English in Japan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Got JLPT N3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Got JLPT N2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Started Learning Python (with MIT 6.00.1x)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Got JLPT N1; Python (with MIT 6.00.2x); Completed JET Programme; Began Direct Hire English Teaching Position&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Started Learning JavaScript; Free Code Camp; The Odin Project&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Began Data Structures and Algorithms ; Edabit; Codewars; Leetcode (Roughly 30 minutes a day for just over a year); Restarted The Odin Project (gave up for some time)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Completed Direct Hire Position; Began Unpaid Internship at a Tech Startup (6 months); Completed Internship; Began Full Time Developer Job at another Tech Startup&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;Full Time Developer at Tech Startup (continued)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I also started a lot of other courses (CS50X, University of Helsinki, and a few other courses) but didn’t get far enough for them to have been noteworthy additions to my skillset. &lt;/p&gt;

&lt;h1&gt;
  
  
  Skilling Up
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Years 1-5: Not really getting anywhere
&lt;/h2&gt;

&lt;p&gt;Years 1-5 were very shaky for me. &lt;/p&gt;

&lt;p&gt;I didn’t spend more than 2-3 hours a week on learning. I regret this to this day, as I would probably be several years ahead in my career had I just taken it seriously. However, I have the attention span of a rock and my focus wavers and disappears completely for weeks at a time. Spoilers: I’ve been trying to write this article for a year.&lt;/p&gt;

&lt;h2&gt;
  
  
  Year 6: Starting to see skills developing
&lt;/h2&gt;

&lt;p&gt;By year 6, I had 2 courses completed: EdX’s MIT 6.00.1x, and 6.00.2x. These courses would go on to become the anchor to all of the Python I know today. They were very hard courses; I would only recommend them after spending a lot of time struggling and trying to not drown in other courses/materials such as &lt;a href="https://exercism.org/"&gt;Exercism.io&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Year 7: Ramping up
&lt;/h2&gt;

&lt;p&gt;Year 7, however, put my anxiety through the roof and had me in panic mode nearly every waking hour. What was I going to do after being an English teacher? Up until then, I was coding for fun, but now I really needed to make a decision. It was do or die. I did not want to end up with only English teaching skills. I love kids, and I love teaching—but I knew that I would forever regret it if I didn’t make a big change soon.&lt;/p&gt;

&lt;p&gt;Thus, I took the plunge and decided to go hard at year 7. &lt;/p&gt;

&lt;p&gt;I restarted The Odin Project and went at it for about 10-15 hours a week, nonstop, for one full year. 90% of my time here was spent fighting my IDE or wondering why my code didn’t work. Turns out this is exactly what it’s like on the job 🙂.&lt;/p&gt;

&lt;p&gt;This disciplined learning got me through the Fundamentals portion, and got me right up until the React portion of the JS track. By now, I had already been doing some simple data structures and algorithms and had also had a taste of CS50X for some computer science goodness. It was time to finally apply to jobs. &lt;/p&gt;

&lt;h2&gt;
  
  
  Year 8: Unwavering Discipline
&lt;/h2&gt;

&lt;p&gt;Nothing interesting to write here. I spent every waking hour thinking of code; thinking of how to solve a problem at hand. I tried really, really hard to write like a developer, think like a developer, and surround myself with tech people. I was determined to &lt;em&gt;fake it till I made it&lt;/em&gt;.  This brings me to...&lt;/p&gt;

&lt;h1&gt;
  
  
  The Job Hunt
&lt;/h1&gt;

&lt;p&gt;From the time I started coding regularly to the time I landed a position, about 3 years had passed. &lt;/p&gt;

&lt;p&gt;Over the course of 9 months. I meticulously tracked every company I applied to on an excel spreadsheet. I wanted to find out where I was getting stuck, why I was getting ignored, and how to improve from each and every rejection.&lt;/p&gt;

&lt;p&gt;However, as good as that sounds on paper, I was no where near mentally prepared for the amount of rejections I would get. There were numerous periods where I wanted to give up. It was crushing. I had sleepless nights as I awaited results, and other nights wondering if I would need to go back to Canada.&lt;/p&gt;

&lt;p&gt;The results of my job hunt consisted of:&lt;/p&gt;

&lt;p&gt;157 applications&lt;/p&gt;

&lt;p&gt;13 interviews&lt;/p&gt;

&lt;p&gt;5 second interviews&lt;/p&gt;

&lt;p&gt;3 final interviews&lt;/p&gt;

&lt;p&gt;and....&lt;/p&gt;

&lt;p&gt;1 offer&lt;/p&gt;




&lt;p&gt;Ultimately, I landed a position at a startup, where I am wearing many hats—I am a project manager, a software developer, and a simultaneous interpreter. I’m happy to be here, as I get to use all of my skills acquired throughout my years as a teacher.&lt;/p&gt;

&lt;p&gt;If I were to describe the top three things that helped me on this career switch, my shoutouts would go to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Odin Project&lt;/li&gt;
&lt;li&gt;CS50&lt;/li&gt;
&lt;li&gt;Twitter's #100DaysOfCode challenge &lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;I hope you were able to learn something from this article. Grit can take us very, very far. I am still very much a baby to this journey; switching careers wasn't the end goal, it was the first step.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>productivity</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Fixing "Require statement not part of import statement.eslint[@typescript-eslint/no-var-requires]"</title>
      <dc:creator>Dave</dc:creator>
      <pubDate>Sun, 03 Apr 2022 01:27:43 +0000</pubDate>
      <link>https://dev.to/msoup/fixing-require-statement-not-part-of-import-statementeslinttypescript-eslintno-var-requires-25n3</link>
      <guid>https://dev.to/msoup/fixing-require-statement-not-part-of-import-statementeslinttypescript-eslintno-var-requires-25n3</guid>
      <description>&lt;p&gt;When you run ESLint through a typescript file, you may run into this warning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Require statement not part of import statement.eslint[@typescript-eslint/no-var-requires]&lt;span class="o"&gt;(&lt;/span&gt;https://typescript-eslint.io/rules/no-var-requires&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  History
&lt;/h1&gt;

&lt;p&gt;JavaScript from ES2015 onwards uses imports as part of the language specification. TypeScript will compile them into environment specific forms as needed. The reason for this is because require([]) and require(””) are environment specific and are more difficult to statically analyze. &lt;/p&gt;

&lt;h1&gt;
  
  
  Severity
&lt;/h1&gt;

&lt;p&gt;This is for the most part only a syntactical rule. If you don't care about TypeScript module syntax, then you will not need this rule.&lt;/p&gt;

&lt;h1&gt;
  
  
  Solution 1: Remove the variable
&lt;/h1&gt;

&lt;h3&gt;
  
  
  ❌ Incorrect
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&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;foo&lt;/span&gt;&lt;span class="dl"&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;foo&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;foo&lt;/span&gt;&lt;span class="dl"&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;foo&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;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;✅ Correct&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;foo&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;foo&lt;/span&gt;&lt;span class="dl"&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;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Solution 2: Ignore the instance
&lt;/h1&gt;

&lt;p&gt;Solution 2 is a band-aid solution that tells TS to simply ignore the part of the file that is giving the warning. &lt;/p&gt;

&lt;p&gt;Place this line right before the require statement:&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="k"&gt;*&lt;/span&gt; tslint:disable no-var-requires &lt;span class="k"&gt;*&lt;/span&gt;/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Solution 3: Ignore at file level
&lt;/h1&gt;

&lt;p&gt;Ignore &lt;em&gt;all&lt;/em&gt; instances of var requires at the file level.&lt;/p&gt;

&lt;p&gt;At the top of the file, place one of the below two lines:&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="k"&gt;*&lt;/span&gt; eslint @typescript-eslint/no-var-requires: &lt;span class="s2"&gt;"off"&lt;/span&gt; &lt;span class="k"&gt;*&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;/&lt;span class="k"&gt;*&lt;/span&gt; eslint-disable @typescript-eslint/no-var-requires &lt;span class="k"&gt;*&lt;/span&gt;/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Solution 4: Ignore at project level
&lt;/h1&gt;

&lt;p&gt;Ignore every instance of variable requires usage&lt;/p&gt;

&lt;p&gt;In your &lt;code&gt;eslintrc.js&lt;/code&gt; file, make your own rule to disable it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;module.exports &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  ...
  rules: &lt;span class="o"&gt;{&lt;/span&gt;
    ...
    &lt;span class="s1"&gt;'@typescript-eslint/no-var-requires'&lt;/span&gt;: 0,
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Happy ESlinting&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>eslint</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Making PyTest in repl.it work</title>
      <dc:creator>Dave</dc:creator>
      <pubDate>Wed, 30 Mar 2022 07:44:03 +0000</pubDate>
      <link>https://dev.to/msoup/pytest-in-replit-97m</link>
      <guid>https://dev.to/msoup/pytest-in-replit-97m</guid>
      <description>&lt;p&gt;This is a very short guide on how to run PyTest within a &lt;a href="http://repl.it"&gt;repl.it&lt;/a&gt; environment. It covers some of the issues I dealt with in trying to get PyTest working on repl.it instances. For the purposes of this article, I will be using the below 4 line script, named &lt;code&gt;test_main.py&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Making a Simple Test
&lt;/h1&gt;

&lt;p&gt;Pytest looks at your filenames and functions—anything that begins with “test” are eligible targets for pytest to run on. &lt;/p&gt;

&lt;p&gt;Make a file called &lt;code&gt;test_main.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Write two assertions—if you can’t think of any, just copy the below in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_one_equals_one&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
  &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_one_does_not_equal_two&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
  &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Using PyTest in repl.it
&lt;/h1&gt;

&lt;p&gt;[True as of April 1, 2022]&lt;/p&gt;

&lt;p&gt;You may have stumbled upon guides where testing was as simple as invoking &lt;code&gt;pytest&lt;/code&gt; on the command line. &lt;/p&gt;

&lt;p&gt;However, doing so in &lt;a href="http://repl.it"&gt;repl.it&lt;/a&gt; will give you the error below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;pytest&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;usr&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nb"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;python3&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;No&lt;/span&gt; &lt;span class="n"&gt;such&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;directory&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This error is because &lt;a href="http://repl.it"&gt;repl.it&lt;/a&gt; instantiates instances within a virtual environment, as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;ImperfectGaseousLocks&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;which&lt;/span&gt; &lt;span class="n"&gt;python&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;runner&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;ImperfectGaseousLocks&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;venv&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nb"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;python&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s see where pip installed pytest...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;ImperfectGaseousLocks&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;which&lt;/span&gt; &lt;span class="n"&gt;pytest&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;runner&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;ImperfectGaseousLocks&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;venv&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nb"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;pytest&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks good to me! So why does pytest try to call python over in /usr/bin/env? How do we call pytest using the correct python? &lt;/p&gt;

&lt;h1&gt;
  
  
  PATH issues
&lt;/h1&gt;

&lt;p&gt;As it turns out, pytest has a shebang: &lt;code&gt;#!/usr/bin/env&lt;/code&gt;. The implication is that when you invoke &lt;code&gt;pytest&lt;/code&gt;, the invocation order becomes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;/home/runner/ImperfectGaseousLocks/venv/bin/pytest invoked&lt;/li&gt;
&lt;li&gt;pytest tells the system to use &lt;code&gt;#!/usr/bin/env&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/usr/bin/env&lt;/code&gt; does not have &lt;code&gt;./python3&lt;/code&gt;, so it yells at us&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Solution 1
&lt;/h1&gt;

&lt;p&gt;Use &lt;code&gt;python -m pytest&lt;/code&gt; in the directory where the test file lies.&lt;/p&gt;

&lt;p&gt;Heading over to &lt;a href="https://docs.python.org/3/using/cmdline.html"&gt;the Python documentation&lt;/a&gt;, you can see this under “interface options”&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="nt"&gt;-m&lt;/span&gt; &amp;lt;module-name&amp;gt;
Search sys.path &lt;span class="k"&gt;for &lt;/span&gt;the named module and execute its contents as the __main__ module.

Since the argument is a module name, you must not give a file extension &lt;span class="o"&gt;(&lt;/span&gt;.py&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt; The module name should be a valid absolute Python module name, but the implementation may not always enforce this &lt;span class="o"&gt;(&lt;/span&gt;e.g. it may allow you to use a name that includes a hyphen&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;

Package names &lt;span class="o"&gt;(&lt;/span&gt;including namespace packages&lt;span class="o"&gt;)&lt;/span&gt; are also permitted. When a package name is supplied instead of a normal module, the interpreter will execute &amp;lt;pkg&amp;gt;.__main__ as the main module. This behaviour is deliberately similar to the handling of directories and zipfiles that are passed to the interpreter as the script argument.

Note This option cannot be used with built-in modules and extension modules written &lt;span class="k"&gt;in &lt;/span&gt;C, since they &lt;span class="k"&gt;do &lt;/span&gt;not have Python module files. However, it can still be used &lt;span class="k"&gt;for &lt;/span&gt;precompiled modules, even &lt;span class="k"&gt;if &lt;/span&gt;the original &lt;span class="nb"&gt;source &lt;/span&gt;file is not available.
If this option is given, the first element of sys.argv will be the full path to the module file &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;while &lt;/span&gt;the module file is being located, the first element will be &lt;span class="nb"&gt;set &lt;/span&gt;to &lt;span class="s2"&gt;"-m"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt; As with the &lt;span class="nt"&gt;-c&lt;/span&gt; option, the current directory will be added to the start of sys.path.

&lt;span class="nt"&gt;-I&lt;/span&gt; option can be used to run the script &lt;span class="k"&gt;in &lt;/span&gt;isolated mode where sys.path contains neither the current directory nor the user’s site-packages directory. All PYTHON&lt;span class="k"&gt;*&lt;/span&gt; environment variables are ignored, too.

Many standard library modules contain code that is invoked on their execution as a script. An example is the timeit module:

python &lt;span class="nt"&gt;-m&lt;/span&gt; timeit &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="s1"&gt;'setup here'&lt;/span&gt; &lt;span class="s1"&gt;'benchmarked code here'&lt;/span&gt;
python &lt;span class="nt"&gt;-m&lt;/span&gt; timeit &lt;span class="nt"&gt;-h&lt;/span&gt; &lt;span class="c"&gt;# for details&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In English, when you use &lt;code&gt;-m&lt;/code&gt;, Python imports PyTest for you to run as a script. This makes sure that relative imports will work as expected, and thus PyTest should too.&lt;/p&gt;

&lt;p&gt;With this, python sees pytest, and pytest ignores the shebang line and executes successfully.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;~/ImperfectGaseousLocks&lt;span class="nv"&gt;$ &lt;/span&gt;python &lt;span class="nt"&gt;-m&lt;/span&gt; pytest
&lt;span class="o"&gt;====================================&lt;/span&gt; &lt;span class="nb"&gt;test &lt;/span&gt;session starts &lt;span class="o"&gt;====================================&lt;/span&gt;
platform linux &lt;span class="nt"&gt;--&lt;/span&gt; Python 3.8.12, pytest-7.1.1, pluggy-0.13.1
rootdir: /home/runner/ImperfectGaseousLocks
collected 2 items                                                                           

test_main.py ..                                                                       &lt;span class="o"&gt;[&lt;/span&gt;100%]

&lt;span class="o"&gt;=====================================&lt;/span&gt; 2 passed &lt;span class="k"&gt;in &lt;/span&gt;0.14s &lt;span class="o"&gt;=====================================&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Solution 2
&lt;/h1&gt;

&lt;p&gt;Change the shebang part of the pytest script!&lt;/p&gt;

&lt;p&gt;The error tells us exactly what’s wrong. A file called “./python3” does not exist. However, what does exist is a file called “python3,” so let’s fix that.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;~/ImperfectGaseousLocks$ vim venv/bin/pytest&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;press &lt;code&gt;i&lt;/code&gt; for insertion mode&lt;/li&gt;
&lt;li&gt;Change &lt;code&gt;/usr/bin/env ./python3&lt;/code&gt; to &lt;code&gt;/usr/bin/env python3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;press esc to exit insertion mode&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:x&lt;/code&gt; to exit and save that script&lt;/li&gt;
&lt;li&gt;Try running pytest alone in the directory where your tests are again
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;~/ImperfectGaseousLocks&lt;span class="nv"&gt;$ &lt;/span&gt;pytest &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;span class="o"&gt;====================================&lt;/span&gt; &lt;span class="nb"&gt;test &lt;/span&gt;session starts &lt;span class="o"&gt;====================================&lt;/span&gt;
platform linux &lt;span class="nt"&gt;--&lt;/span&gt; Python 3.8.12, pytest-7.1.1, pluggy-0.13.1 &lt;span class="nt"&gt;--&lt;/span&gt; /home/runner/ImperfectGaseousLocks/venv/bin/python3
cachedir: .pytest_cache
rootdir: /home/runner/ImperfectGaseousLocks
collected 2 items                                                                           

test_main.py::test_one_equals_one PASSED                                              &lt;span class="o"&gt;[&lt;/span&gt; 50%]
test_main.py::test_one_does_not_equal_two PASSED                                      &lt;span class="o"&gt;[&lt;/span&gt;100%]

&lt;span class="o"&gt;=====================================&lt;/span&gt; 2 passed &lt;span class="k"&gt;in &lt;/span&gt;0.12s &lt;span class="o"&gt;=====================================&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Success!&lt;/p&gt;

&lt;p&gt;PATH issues can be a nightmare to navigate, and we all inevitably encounter silly issues like these once in a while.&lt;/p&gt;

&lt;p&gt;Please comment if you have any questions.&lt;/p&gt;

&lt;p&gt;This is my first ever technical blog post. I welcome any constructive criticism and I'm open to learning from anyone regardless of experience level. Hi!&lt;/p&gt;

</description>
      <category>replit</category>
      <category>python</category>
      <category>pytest</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
