<?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: Charles Best </title>
    <description>The latest articles on DEV Community by Charles Best  (@dverybest).</description>
    <link>https://dev.to/dverybest</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%2F413917%2F3523b649-c70d-4cd4-81da-50bada9489aa.jpeg</url>
      <title>DEV Community: Charles Best </title>
      <link>https://dev.to/dverybest</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dverybest"/>
    <language>en</language>
    <item>
      <title>Removing Duplicate Objects from an Array of Objects</title>
      <dc:creator>Charles Best </dc:creator>
      <pubDate>Wed, 06 Jan 2021 23:44:32 +0000</pubDate>
      <link>https://dev.to/dverybest/removing-duplicate-objects-from-an-array-of-objects-2ap9</link>
      <guid>https://dev.to/dverybest/removing-duplicate-objects-from-an-array-of-objects-2ap9</guid>
      <description>&lt;p&gt;Arrays and Objects are two basic data structure we find ourselves using in our everyday JavaScript coding task. Though they are quite simple to use, as the number of data we use them to store grow very large, we find a need to optimize the time and resources taken to perform operations on them.&lt;/p&gt;

&lt;p&gt;In this article, I will focus on two fast approach to remove duplicate objects from an array of objects.&lt;/p&gt;

&lt;p&gt;Recently, while working on a project, I came across a task that requires to remove duplicates from an array of objects.&lt;/p&gt;

&lt;p&gt;For example, say you have this array of objects and you are tasked to find and remove any duplicates in the 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&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="na"&gt;id&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="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pick meals&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Choose your meal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Checkout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enter a delivery location&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Fast Delivery&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Your order is processed and delivered&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Checkout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enter a delivery location&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&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="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pick meals&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Choose your meal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This task may look trivial at first but as you dive in to code and having optimization in mind, you will find it not as trivial as you thought. Below are two methods different methods to solve this task&lt;/p&gt;
&lt;h2&gt;
  
  
  Method 1 : Using Set
&lt;/h2&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    let data = [
  {
    id: 1,
    title: "Pick meals",
    description: "Choose your meal"
  },
  {
    id: 2,
    title: "Checkout",
    description: "Enter a delivery location"
  },
  {
    id: 3,
    title: "Fast Delivery",
    description: "Your order is processed and delivered"
  },
  {
    id: 2,
    title: "Checkout",
    description: "Enter a delivery location"
  },
 {
    id: 1,
    title: "Pick meals",
    description: "Choose your meal"
  }
] 
  &lt;/code&gt;
  &lt;code&gt;
    
//1st convert all object in the array to string to make it easy to compare them
let result = data.map(item=&amp;gt;JSON.stringify(item));

//pass the array into a Set to remove duplicates
result= new Set(result);

// convert the Set back to array of strings
result = Array.from(result);

// finally convert the array of strings to an array of object
result = result.map(item=&amp;gt;JSON.parse(item));

console.log(result);

  &lt;/code&gt;
&lt;/div&gt;



&lt;p&gt;It is important to stringify the objects first before passing them to the Set object else the Set will compare them by reference instead of by value i.e Since the object are store in different memory location, the Set will see them as different object even though they have the same exact values. &lt;/p&gt;

&lt;h2&gt;
  
  
  Method 2 : Using Objects
&lt;/h2&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    let data = [
  {
    id: 1,
    title: "Pick meals",
    description: "Choose your meal"
  },
  {
    id: 2,
    title: "Checkout",
    description: "Enter a delivery location"
  },
  {
    id: 3,
    title: "Fast Delivery",
    description: "Your order is processed and delivered"
  },
  {
    id: 2,
    title: "Checkout",
    description: "Enter a delivery location"
  },
 {
    id: 1,
    title: "Pick meals",
    description: "Choose your meal"
  }
] 
  &lt;/code&gt;
  &lt;code&gt;
    
//1st create an empty object
let result = {};

//loop through the data array and use one of the keys of objects in the array to create properties in the result object 
data.forEach(item=&amp;gt;result[item.id] = item)

// extract back the array from the object
result= Object.values(result);

console.log(result)

  &lt;/code&gt;
&lt;/div&gt;


&lt;p&gt;Both methods works well so far that the duplicate objects are exactly the same. &lt;strong&gt;Method 2&lt;/strong&gt; apart from being shorter executes faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  Note
&lt;/h2&gt;

&lt;p&gt;In some rare cases, if the definition of duplicate is altered for example  given the &lt;br&gt;
below data and&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;data&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="na"&gt;id&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="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pick meals&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Choose your meal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hey&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enter a delivery location&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Fast Delivery&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Your order is processed and delivered&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Checkout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enter a delivery location&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Method 2&lt;/strong&gt; will not be the best if duplicate means itemA is exactly the same as ItemB and some object has the same id but have some other properties different, in this case &lt;strong&gt;Method 1&lt;/strong&gt; is best to use. Similarly if duplicate means itemA has exactly the same 'id' as ItemB regardless of other properties then &lt;strong&gt;Method 2&lt;/strong&gt; is the best for to use.&lt;/p&gt;

&lt;p&gt;I hope you find this article useful, if you like the content, feel free to stay in touch, follow me on &lt;a href="https://twitter.com/_DVERYBEST"&gt;twitter@_DVERYBEST&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>TDD &amp; Automated Testing in JavaScript using Jest</title>
      <dc:creator>Charles Best </dc:creator>
      <pubDate>Wed, 04 Nov 2020 23:06:42 +0000</pubDate>
      <link>https://dev.to/dverybest/tdd-automated-testing-in-javascript-using-jest-3g4a</link>
      <guid>https://dev.to/dverybest/tdd-automated-testing-in-javascript-using-jest-3g4a</guid>
      <description>&lt;p&gt;TDD (Test Driven Development), automated testing, unit test, integration test, are some of the popular keywords you find in recent software development job descriptions/requirements, most especially JavaScript (react, react-native, nodejs) related jobs.&lt;/p&gt;

&lt;p&gt;In this article, I will explain how to get started on writing automated test in JavaScript using &lt;strong&gt;Jest&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  JEST
&lt;/h2&gt;

&lt;p&gt;As stated in their &lt;a href="https://jestjs.io/" rel="noopener noreferrer"&gt;official landing page&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Jest is a delightful JavaScript Testing Framework with a focus on simplicity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Jest is one of the most popular JavaScript testing framework and can be used to write automated test for almost everything JavaScript. It is easy to understand and get started with.&lt;/p&gt;

&lt;p&gt;Before we dive into writing codes, I will explain some important concepts&lt;/p&gt;

&lt;h3&gt;
  
  
  Automated Testing
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Automated testing or test automation is a method in software testing that makes use of special software tools to control the execution of tests and then compares actual test results with predicted or expected results.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Simply put it is a way of testing our code(software) in other to compare the actual test results with predicted or expected results without manually going through the code.&lt;/p&gt;

&lt;p&gt;This help ensure our code is bug-free at all point as tests will fail if bug is introduce to the code at any point.&lt;/p&gt;

&lt;p&gt;Automated testing can be broken into two main types namely: &lt;strong&gt;unit tests&lt;/strong&gt; and &lt;strong&gt;integration tests&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Unit tests simulate all possible outputs of a single function or component, given various inputs. They mock all external function calls in order to achieve a pure test of only that one function's logic.&lt;br&gt;
Integration tests check to make sure that various functions or components are working together properly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now having understand these concepts, let dive in writing test codes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set-Up
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;You need to already have &lt;a href="https://nodejs.org/" rel="noopener noreferrer"&gt;nodeJs&lt;/a&gt; installed/setup to follow along.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new folder "test_master_class"&lt;/li&gt;
&lt;li&gt;On your terminal cd into "test_master_class" and run the command &lt;code&gt;npm init&lt;/code&gt;  to scaffold &lt;a href="https://nodejs.dev/learn/the-package-json-guide" rel="noopener noreferrer"&gt;package.json&lt;/a&gt;. &lt;em&gt;Enter "jest" when asked for "test command" during scaffolding.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;npm install --save-dev jest&lt;/code&gt; to install jest as a dev dependency.&lt;/li&gt;
&lt;li&gt;Finally open your folder in your favorite editor. &lt;/li&gt;
&lt;/ol&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqzobej672798c6olun3p.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqzobej672798c6olun3p.png" alt="Alt vs code showing package.json"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now to write our first test code, which will be a very simple demo, inside your working folder, create a folder named "test" and inside the folder create a file named "index.test.js" and add the following code&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
to run the test, on your terminal type &lt;code&gt;npm test&lt;/code&gt;and press enter; and you would get the follow output&lt;br&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User@CharlesBest MINGW32 ~/Documents/MyJavaScript/test_master_class
$ npm test
&amp;gt; test_master_class@1.0.0 test C:\Users\User\Documents\MyJavaScript\test_master_class
&amp;gt; jest

PASS test/index.test.js
  test to see if test enviroment is properly set up
    √ test hello word (4 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        5.842 s
Ran all test suites.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;From the code above in "index.test.js", &lt;strong&gt;describe&lt;/strong&gt; is a global function from jest. It takes two main arguments, first a string that describes the tests to be contained in the describe block and the second argument is an an anonymous function containing the actual tests to be done. One of the main functions of the describe function is to group related test together. Similarly, &lt;strong&gt;test&lt;/strong&gt;  is also a global function from jest, it contains the actual test to be done. Also &lt;strong&gt;expect&lt;/strong&gt; is a global function from jest, it takes the actual test result and compare it to an expected output contained in matchers like &lt;strong&gt;toBe&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A comprehensive list of other matchers like toBe, toEqual, toBeTruthy, etc. and their functions can be found on the &lt;a href="https://jestjs.io/docs/en/using-matchers" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now that we are clear with the basics of testing using jest, lets write unit and integration test for real scenarios.&lt;/p&gt;
&lt;h2&gt;
  
  
  Getting Real
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Scenario&lt;/strong&gt;: Let's build a simple calculator system, this system should be able to increment and decrement a counter with a given number, get the current value of the counter after each operation and the time taken to perform each operation, we should be able to reset the value of the count.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: if we are to follow TDD principles, we are expect to write test first before the actual code.&lt;/p&gt;

&lt;p&gt;In our test folder create a file named "calculator_service.test.js"  and add the following code and run the test on the terminal using &lt;code&gt;npm test&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
we get the output below&lt;br&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PASS test/index.test.js (6.212 s)
FAIL test/calculator_service.test.js
  ● Test suite failed to run
    Cannot find module '../service/calculator' from 'test/calculator_service.test.js'
    &amp;gt; 1 | const {add,subtract,getTime} = require('../service/calculator');
        |                                ^
      2 |
      3 | describe('test to see if functions are defined', () =&amp;gt; {
      4 |

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:306:11)
      at Object.&amp;lt;anonymous&amp;gt; (test/calculator_service.test.js:1:32)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        40.271 s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

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

&lt;/div&gt;


&lt;p&gt;an error occurs making the test in "calculator_service.test.js" not to run and this is normal as "../service/calculator" we required does not exist yet. &lt;/p&gt;

&lt;p&gt;In the root folder, create a folder "service", inside the folder add a new file "calculator.js" and run the test again, this time we get the below output&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PASS test/index.test.js
FAIL test/calculator_service.test.js
  ● test to see if functions are defined › test add function
    expect(received).not.toBeUndefined()
    Received: undefined
      4 |
      5 |     test("test add function", () =&amp;gt; {
    &amp;gt; 6 |         expect(add).not.toBeUndefined()
        |                         ^
      7 |     })
      8 |
      9 |     test("test add function", () =&amp;gt; {
      at Object.&amp;lt;anonymous&amp;gt; (test/calculator_service.test.js:6:25)
  ● test to see if functions are defined › test add function
    expect(received).not.toBeUndefined()
    Received: undefined
       8 |
       9 |     test("test add function", () =&amp;gt; {
    &amp;gt; 10 |         expect(subtract).not.toBeUndefined()
         |                              ^
      11 |     })
      12 |
      13 |     test("test add function", () =&amp;gt; {
      at Object.&amp;lt;anonymous&amp;gt; (test/calculator_service.test.js:10:30)
  ● test to see if functions are defined › test add function
    expect(received).not.toBeUndefined()
    Received: undefined
      12 |
      13 |     test("test add function", () =&amp;gt; {
    &amp;gt; 14 |         expect(getTime).not.toBeUndefined()
         |                             ^
      15 |     })
      16 | });
      at Object.&amp;lt;anonymous&amp;gt; (test/calculator_service.test.js:14:29)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       3 failed, 1 passed, 4 total
Snapshots:   0 total
Time:        10.71 s
Ran all test suites.
npm ERR! Test failed.  See above for more details.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This time our test ran successfully, but the test cases failed. From the output you can see what was expected and what was received.&lt;br&gt;
Next, we create the expected functions by adding the following code to "../service/calculator.js" and run the test again.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
this time, all the test passes as shown in the output below&lt;br&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm test
&amp;gt; test_master_class@1.0.0 test C:\Users\User\Documents\MyJavaScript\test_master_class
&amp;gt; jest
PASS test/index.test.js (5.568 s)
PASS test/calculator_service.test.js (9.661 s)

Test Suites: 2 passed, 2 total
Tests:       4 passed, 4 total
Snapshots:   0 total
Time:        16.167 s
Ran all test suites.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We can add further test cases to "calculator_service.test" as much as required. Example test to check if the functions returns a correct output when given an input.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm test
&amp;gt; test_master_class@1.0.0 test C:\Users\User\Documents\MyJavaScript\test_master_class
&amp;gt; jest
PASS test/index.test.js
PASS test/calculator_service.test.js

Test Suites: 2 passed, 2 total
Tests:       7 passed, 7 total
Snapshots:   0 total
Time:        9.401 s
Ran all test suites.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;So far all test done so far are unit tests. Now lets write integration tests to test the functions coupled together. &lt;/p&gt;

&lt;p&gt;In our test folder, create a new file "calculator_controller.test.js" and add the following code&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Next in the root folder, create a folder "controller", inside the folder add a new file "calculator.js" and add the following code then run the test again&lt;br&gt;&lt;/p&gt;

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
if everything goes well, you should have the below output&lt;br&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm test
&amp;gt; test_master_class@1.0.0 test C:\Users\User\Documents\MyJavaScript\test_master_class
&amp;gt; jest

PASS test/index.test.js
PASS test/calculator_service.test.js
PASS test/calculator_controller.test.js

Test Suites: 3 passed, 3 total
Tests:       12 passed, 12 total
Snapshots:   0 total
Time:        6.974 s
Ran all test suites.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In "calculator_controller.test.js", you many notice that I introduced two new functions &lt;strong&gt;beforeAll&lt;/strong&gt; and &lt;strong&gt;afterAll&lt;/strong&gt;, they are jest functions used to perform some operations before and after your test runs respectively.&lt;/p&gt;

&lt;p&gt;TDD is all about writing "automated test" first, before writing codes that will pass the test; this helps ensure codes are broken into testable units which in turn reduce code duplication and bugs. &lt;/p&gt;

&lt;p&gt;To pull the whole code, goto &lt;a href="https://github.com/Dverybest/test_master_class" rel="noopener noreferrer"&gt;the github link&lt;/a&gt;&lt;br&gt;
I hope you find this article useful, if you like the content, feel free to stay in touch, follow me on &lt;a href="https://twitter.com/_DVERYBEST" rel="noopener noreferrer"&gt;twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>testing</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Basic JavaScript Interview Questions</title>
      <dc:creator>Charles Best </dc:creator>
      <pubDate>Mon, 19 Oct 2020 01:17:31 +0000</pubDate>
      <link>https://dev.to/dverybest/basic-javascript-interview-questions-3491</link>
      <guid>https://dev.to/dverybest/basic-javascript-interview-questions-3491</guid>
      <description>&lt;p&gt;Hi everyone! So I had couple of interviews this year for JavaScript software development roles, and I felt its worth writing an article on some of the interview questions I was asked.&lt;br&gt;
In this article I will write some of the questions I was asked and the answers to them.&lt;/p&gt;
&lt;h2&gt;
  
  
  Explain data structure
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Data structure is a data organization, management, and storage format that enables efficient access and modification. A data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Simply put, data structure is a defined format/way of storing and managing a collection of data.&lt;/p&gt;
&lt;h2&gt;
  
  
  List examples of data structures, explain and implement one
&lt;/h2&gt;

&lt;p&gt;some common data structures includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;array&lt;/li&gt;
&lt;li&gt;linked list&lt;/li&gt;
&lt;li&gt;double linked list&lt;/li&gt;
&lt;li&gt;stack&lt;/li&gt;
&lt;li&gt;queue&lt;/li&gt;
&lt;li&gt;hash map&lt;/li&gt;
&lt;li&gt;etc&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;I will explain and implement a stack.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A stack is a  linear data structure that stores data in a LIFO(Last in First Out) manner, i.e. the item added/inserted last is the first item to be accessed. Data in a stack can only be accessed from one end(top of the stack).&lt;br&gt;
A Stack data structure supports only two type of operation namely &lt;code&gt;PUSH&lt;/code&gt;(insert/add item) and &lt;code&gt;POP&lt;/code&gt;(delete/remove item) operation.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Implementation&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Stack {
    constructor() {
        this.top = -1;
        this.store = [];
    }

    pop = () =&amp;gt;{
        if(this.top&amp;lt;0){
            return null;
        }
        let poppedItem = this.store[this.top];
        this.store.length = --this.top+1;
        return poppedItem;
    }

    push = (item)=&amp;gt;{
        this.store[++this.top] = item;
        return;
    }

    getTop = ()=&amp;gt;{
        return this.store[this.top];
    }
}

let myStack = new Stack();

myStack.push("10");
myStack.push("34");
myStack.push("17");
console.log(myStack.getTop());//output 17
console.log(myStack.pop());
console.log(myStack.getTop());//output 34
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explain Closure with code example
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A closure is a function having access to the parent scope, even after the parent function has closed.&lt;br&gt;
&lt;em&gt;implementation&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var add = (function(){
    let accumulator = 0;
    return function(value){
        return accumulator+=value;
    }
})();

console.log(add(3)); //output 3
console.log(add(5)); //output 8
console.log(add(7)); //output 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Closure makes it possible for functions to have private variable. E.g in the code above, the function returned by the anonymous function is still able to access the &lt;code&gt;accumulator&lt;/code&gt; variable even though the anonymous function is done executing. &lt;/p&gt;

&lt;h2&gt;
  
  
  Explain Asynchronicity in JavaScript with code example
&lt;/h2&gt;

&lt;p&gt;JavaScript is single threaded, meaning codes are executed sequentially/synchronously(line by line one after the other). Asynchronous JavaScript enables code execution without blocking the main thread, i.e code execute without blocking/stopping other code from executing immediately while its still running/executing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;code example&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("start");
new Promise((resolve,reject)=&amp;gt;{
    resolve({data:'hello world'});
}).then(res=&amp;gt;{
    console.log(res);
})
console.log("end");
//outputs
//start
//end
//{ data: 'hello world' }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, &lt;code&gt;console.log("end")&lt;/code&gt; executes before the &lt;code&gt;promise&lt;/code&gt; even though the &lt;code&gt;promise&lt;/code&gt; started executing first. This is because the &lt;code&gt;promise&lt;/code&gt; is asynchronous and did not block the main thread allowing &lt;code&gt;console.log("end")&lt;/code&gt; to execute while it is executing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explain Higher Order Functions.
&lt;/h2&gt;

&lt;p&gt;Higher order functions are functions that take other functions as arguments and/or a function that returns a function.&lt;br&gt;
&lt;em&gt;code example&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function logger(){
console.log("Hello world!");
}
setTimeOut(logger,2000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above &lt;code&gt;setTimeOut&lt;/code&gt; is a higher other function that takes the function &lt;code&gt;logger&lt;/code&gt; as an argument.&lt;/p&gt;

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

&lt;p&gt;I hope you find this article useful and it helps you prepare for interview.&lt;br&gt;
If you like the content, feel free to stay in touch, follow me on &lt;a href="https://twitter.com/_dverybest"&gt;twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
      <category>reactnative</category>
    </item>
  </channel>
</rss>
