My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Assertion Testing in NodeJs 1

Backend Papa's photo
Backend Papa
·Sep 17, 2020·

2 min read

It's your wonderful back-end dad, Charlse Nodejs. This is my first ever Nodejs tutorial, and I'm happy to help you all learn the deepest part of NodeJs. Let's get started.

What 'd heck is Assertion testing

Assertion checking is a boolean expression used to search for error within a particular part of a computer program.Error is disclosed when argument becomes is false.

Nodejs provides us with a global module called Assert used in our nodeJs application to search for errors. The Assert module is very useful for detecting errors, particularly in robust applications. Imagine how stressful it would be to debug errors in a large NodeJs project

To use the Assert module we first require it into our node app file.

index.js
const Assert =require('assert').strict

This line of code uses require to import the Assert module.

In this fast example we will compare the content of two array and check if contents in Array A is equal to contents in Array B .

console.log(assert.deepEqual([1,2],[1,2]))

when you run

node index.js

in the console or terminal,you get nothing,

###Terminal

Note that if there are no errors, NodeJs's Assert will reveal nothing in the terminal, this tells us our program is OK. Now, let 's say I was writing my favorite code, and instead of a number I typed 1 as a string.

console.log(assert.deepEqual([1,2],["1",2]))

lets see what happens...

###Terminal

assert.js:103
  throw new AssertionError(obj);
  ^

AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected

  [
+   1,
-   '1',
    2
  ]

Good gracious, the program shows an argument error, .The output shows us the real figure marked by a + sign and the error marked by a -.

 [
+   1,
-   '1',
    2
  ]

This is just a simple example of how critical Assert can save us a lot of debugging by showing the bug in our program. In our next episode, we will address the methods of assertion: equal(),deepEqual(),notDeepEqual(),notEqual() Going back to my PC ... See you later!