<?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: Damien Maillard</title>
    <description>The latest articles on DEV Community by Damien Maillard (@dmail).</description>
    <link>https://dev.to/dmail</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%2F609906%2F427acc29-9a65-4f59-b3c3-3b48c6cdee74.jpeg</url>
      <title>DEV Community: Damien Maillard</title>
      <link>https://dev.to/dmail</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dmail"/>
    <language>en</language>
    <item>
      <title>I am too lazy for a test framework</title>
      <dc:creator>Damien Maillard</dc:creator>
      <pubDate>Tue, 28 Jun 2022 14:41:15 +0000</pubDate>
      <link>https://dev.to/dmail/i-am-too-lazy-for-a-test-framework-92f</link>
      <guid>https://dev.to/dmail/i-am-too-lazy-for-a-test-framework-92f</guid>
      <description>&lt;p&gt;When a task requires too much effort I want to make it simpler. I also like to automate things and use guidelines that help to build things faster with less effort. Certainly a developer characteristic.&lt;/p&gt;

&lt;p&gt;I am a JavaScript developer. I like tests because they can have a high return on investment. You write something once and it will be executed by a machine in the future maybe thousands of times ensuring parts of your code still behaves as expected.&lt;/p&gt;

&lt;p&gt;There are several JavaScript frameworks dedicated to testing. Let’s see a JavaScript example with the code below taken from Jasmine documentation.&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="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A suite is just a function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;and so is a spec&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="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;When you read that code you need to know things that are not part of standard JavaScript.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;describe&lt;/code&gt; and &lt;code&gt;it&lt;/code&gt; functions are magically accessible.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;describe&lt;/code&gt; and &lt;code&gt;it&lt;/code&gt; receive functions that will be executed somehow at some point.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My main issue is that you cannot start writing tests without first learning the testing framework. I’ve tried for a long time to bend my mind and my code to various test frameworks. In the end I always had to use shortcuts or felt unsatisfied with my test files.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I feel unsatisfied with my tests.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Until…&lt;/p&gt;

&lt;p&gt;One day I saw a project testing if the &lt;code&gt;const&lt;/code&gt; keyword was available or not. I don’t remember exactly what it was. The goal was to know which es6 features were available in browsers. A suite of test files had the purpose of checking if const was available. One of the test files in the suite, as I remember it, is available below.&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;a&lt;/span&gt; &lt;span class="o"&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;That’s it.&lt;/p&gt;

&lt;p&gt;For me &lt;code&gt;describe&lt;/code&gt; and &lt;code&gt;it&lt;/code&gt; were mandatory to write tests but this file opened my mind.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Executing the file throws: const not available&lt;/li&gt;
&lt;li&gt;Executing the file does not throw: const available&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I started to realize a test file could be just plain JavaScript. JavaScript that I am used to write every day. From this revelation I wanted to write much simpler tests.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A test file could be written using only standard JavaScript.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To give you an example, here is a file named &lt;em&gt;animals.js&lt;/em&gt;:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;countDogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animals&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="nx"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animal&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;animal&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&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;Let’s say, for the purpose of our example, there is a mistake in the &lt;em&gt;animals.js&lt;/em&gt; file: instead of counting dogs we count cats:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;countDogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animals&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="nx"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animal&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;animal&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&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;We could test &lt;code&gt;countDogs&lt;/code&gt; using a “standard” file named &lt;em&gt;animals.test.js&lt;/em&gt;:&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;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;countDogs&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./animals.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;actual&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;countDogs&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&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;expected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&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;actual&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`countDogs should return &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, got &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;actual&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To execute this test in a browser, create an html file and open it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;countDogs test&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"utf-8"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"icon"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"data:,"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"module"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./animals.test.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember the mistake we did in &lt;code&gt;countDogs&lt;/code&gt; ? Because we count cats instead of dogs we receive &lt;code&gt;3&lt;/code&gt; instead of &lt;code&gt;2&lt;/code&gt;, making the test fail. See the output in chrome DevTools below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CzcS0Bky--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfrmjmjewhdvhvcijd3b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CzcS0Bky--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfrmjmjewhdvhvcijd3b.png" alt="Image description" width="700" height="318"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to execute the test with Node.js, execute the file with node command. If you have never executed a file using import syntax with Node.js, check &lt;a href="https://nodejs.org/docs/latest-v13.x/api/esm.html#esm_enabling"&gt;https://nodejs.org/docs/latest-v13.x/api/esm.html#esm_enabling&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;node ./animals.test.js
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SB1ODlPc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sv0jmulyx5tk2kfahjop.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SB1ODlPc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sv0jmulyx5tk2kfahjop.png" alt="Image description" width="588" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some benefits started to rise:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No concept to learn: reuses basic syntax/apis we already know.&lt;/li&gt;
&lt;li&gt;Executing a test file is like executing a standard file: uncaught error are logged and stops execution, otherwise it’s good.&lt;/li&gt;
&lt;li&gt;Writing test files is like writing a standard file.
Debugging a test file is like executing a standard file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See the point? Once you know how to code in standard JavaScript you know how to write tests. You can start writing simpler tests right now, nothing new to learn.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A test file is a standard file&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At this point it works for one simple test file with basic assertions. Before seeing how to use this way of writing test for more complex scenarios let’s review some impacts.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test files needs less context switching because they are standard files. For instance you will likely reuse ESLint config or reuse standard file execution script to execute test files.&lt;/li&gt;
&lt;li&gt;You can execute and debug test files in isolation as you would execute a standard file&lt;/li&gt;
&lt;li&gt;An error, even an assertion error fails the whole test file. It means you can know the number of failing test files, not the number of failed assertions. In practice I find more comfortable to stop on first error because an error reflects something that is wrong. I prefer to focus on one thing at a time and fix it. Otherwise you are often flooded by X assertions that are failing just because the first one failed.&lt;/li&gt;
&lt;li&gt;Test files becomes more pleasant to write and read. It can open the door to messy code because your are not under the guidance of a testing framework. But I’ve seen many bad test files because testing framework constraints are misunderstood or too painful to follow. Here you can ensure code quality as you do with you other JavaScript files (ESLint, review and general guidelines such as &lt;a href="https://github.com/goldbergyoni/nodebestpractices/tree/061bd10c2a4e2ba3407d9e1205b0fe702ef82b57#-43-structure-tests-by-the-aaa-pattern"&gt;Arrange-Act-Assert pattern&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Testing framework constraints are misunderstood or too painful to follow.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you want to write tests as suggested in this article, at larger scale, you will likely need two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An assertion library&lt;/li&gt;
&lt;li&gt;A test runner to execute JavaScript files and report how many have failed (thrown an error).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Assertion library
&lt;/h2&gt;

&lt;p&gt;Not every assertion is as simple as using comparison operator used in the test file example.&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;actual&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`countDogs should return &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, got &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;actual&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Comparison operator is great to compare primitives values but is unadapted to compare composite values such as an object or an array.&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;toto&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;toto&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&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="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// false!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will need a tool to make these comparison with accuracy. There are many assertion libraries made to do this job: just type “assertion library” on Google to find some. While considering an assertion library, keep in mind that simplicity is important.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“equal() is my favorite assertion. If the only available assertion in every test suite was equal(), almost every test suite in the world would be better for it.”&lt;/p&gt;

&lt;p&gt;Eric Elliot in &lt;a href="https://medium.com/javascript-scene/rethinking-unit-test-assertions-55f59358253f"&gt;Rethinking Unit Test Assertion&lt;/a&gt; -&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With that in mind, I made an opinionated assertion library. The concept holds in one function that can be described in one sentence: &lt;strong&gt;Compare two values, throw if they differ&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/jsenv/assert"&gt;https://github.com/jsenv/assert&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test runner
&lt;/h2&gt;

&lt;p&gt;Here you need something capable to execute one or many JavaScript files and tells you if any have thrown an error. I’ve written a tool capable of executing files either in Chromium, Firefox, Webkit or Node.js. It was designed to handle many executions and tells you how it goes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/jsenv/jsenv-core"&gt;https://github.com/jsenv/jsenv-core&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YY1Gu8P0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e19ooxpmoe9f5jox7z5u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YY1Gu8P0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e19ooxpmoe9f5jox7z5u.png" alt="Image description" width="397" height="289"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The image above was taken after fixing &lt;em&gt;animals.js&lt;/em&gt; to show the logs when tests are ok.&lt;/p&gt;

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

&lt;p&gt;I just said I wanted to be freed from the constraints of a testing framework and now I suggest you to use a tool that can look like a framework! But there is a fundamental difference: with what I suggest, your test files are independent from a framework. They are standard JavaScript. In that regard, they can be executed by everything capable to execute a JavaScript file.&lt;/p&gt;

&lt;p&gt;You’re all set!&lt;/p&gt;

&lt;p&gt;Happy coding or testing that’s kind of the same now :)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Link to original post from 2020 on medium: &lt;a href="https://medium.com/@DamienMaillard/i-am-too-lazy-for-a-test-framework-ca08d216ee05"&gt;https://medium.com/@DamienMaillard/i-am-too-lazy-for-a-test-framework-ca08d216ee05&lt;/a&gt;. (Reposted because I don't use medium anymore)&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>testing</category>
    </item>
    <item>
      <title>Car keys toggle it right !</title>
      <dc:creator>Damien Maillard</dc:creator>
      <pubDate>Fri, 09 Apr 2021 13:30:32 +0000</pubDate>
      <link>https://dev.to/dmail/car-keys-toggle-it-right-45dh</link>
      <guid>https://dev.to/dmail/car-keys-toggle-it-right-45dh</guid>
      <description>&lt;p&gt;Sometimes I feel that a way of doing things is better than another but I struggle to explain why. For example, I don't like when a function, especially a public api, uses "toggle" behaviour. &lt;/p&gt;

&lt;p&gt;To illustrate, let's make a public api controlling if volume is muted or not.&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;let&lt;/span&gt; &lt;span class="nx"&gt;muted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mute&lt;/span&gt; &lt;span class="o"&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;muted&lt;/span&gt; &lt;span class="o"&gt;=&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;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;unmute&lt;/span&gt; &lt;span class="o"&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;muted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&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 past, I got tempted to merge &lt;code&gt;mute&lt;/code&gt; and &lt;code&gt;unmute&lt;/code&gt; into a "smart" toggle 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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;muted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toggleMute&lt;/span&gt; &lt;span class="o"&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;muted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;muted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&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;muted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only to realize that first version was better. But I was struggling to explain why to other developers. And that lasted for long. From time to time, I was wondering what could be the "perfect" example to illustrate my feelings.&lt;/p&gt;

&lt;p&gt;Until one day. &lt;/p&gt;

&lt;p&gt;We where walking in the city with my girlfriend, which is also a dev.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; Do you remember when I told you I don't like when api uses toggle?&lt;br&gt;
&lt;strong&gt;Her:&lt;/strong&gt; I do, yes&lt;br&gt;
&lt;strong&gt;Me:&lt;/strong&gt; I was thinking again how to illustrate that... I like when one button do one thing. Toggle is not user friendly but I still can't explain why with a simple example.&lt;br&gt;
&lt;strong&gt;Her:&lt;/strong&gt; &lt;em&gt;Thinks...&lt;/em&gt; Got it! It's like when you want to lock your car... You don't want to know if your car is locked before pressing the button. You just want to lock your car by pressing a button. Maybe it was useless because the car was already locked but that's not what matters for you. With a toggle you would have to remember if the car is locked otherwise you could unlock it by mistake and have to lock it back.&lt;/p&gt;

&lt;p&gt;I don't know if that rings a bell for you, but for me it illustrates simply why I prefer having 2 buttons: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one to lock the car&lt;/li&gt;
&lt;li&gt;an other to unlock the car&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Applied to a car, we don't want a toggle because user would have to keep in memory the state of the car.&lt;br&gt;
Applied to a program, we don't want a toggle function because  code outside have to remember the state as well.&lt;/p&gt;

</description>
      <category>design</category>
      <category>codequality</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
