Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of assert in NodeJS

Tags:

node.js

assert

I'm new to nodejs, I follow all the steps in the documentation. First I study and test the assert function of node, I just want to know what is the purpose of using assert? if no error there's no output, but if you have an error, there's an output saying AssertError etc.

I want to know, when and what is the purpose of using assert?

like image 483
John Rey M. Baylen Avatar asked Apr 23 '17 13:04

John Rey M. Baylen


People also ask

What is assert in Javascript?

The assert() method writes a message to the console if an expression evaluates to false .

How do you use assert?

Definition and UsageThe assert keyword is used when debugging code. The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError. You can write a message to be written if the code returns False, check the example below.

What is assert strictEqual?

The assert. strictEqual() method tests if two values are equal, using the === operator. If the two values are not equal, an assertion failure is being caused, and the program is terminated. The === operator tests if the values are equal and if their types are equal.

What is assert in MongoDB?

Assertions are errors that may occur on a MongoDB server from time to time. Administrators need to promptly capture these errors and understand their nature, so that they can rapidly resolve them and prevent any negative impact on database performance.


2 Answers

In any programming language errors are an issue. Whether by human error or poor design. The assert module in node.js is used to test the behavior of a function and reduce the creation of "buggy" code, this also promotes design thinking.

In most cases assertions are used in unit testing. This takes a unit of code, be it a function, method etc and runs multiple tests on it. These test the value that a function generates(actual) vs what we expect the function to provide.

An example of assertions:

"use strict";

//Run the code and change the values of x and y (to equal 42) to test the assert module.
const x = 18;
const y = 20;

//We have to import our assert module
var assert = require("assert");

//Calculates the anser to life (42)
var life = function(a,b){
    return a + b;
 };

//Overwrite the variable of result to equal the return.
result = life(x,y);

//Change the comments below to see the difference between the two values
assert.deepEqual(result, 42);
//assert.fail(result, 42, "", '<');

From a technical point of view, developers should write their tests before they start to write their code. This is a form of top-down development strategy, which allows developers to understand the functional requirements of the software. Therefore when writing your assertions you obtain the parameters you require and what results you expect. Thus making the logic the only hurdle.

like image 89
wattry Avatar answered Nov 20 '22 04:11

wattry


Assert is used to write test suites for your apps. This way, you can easily test your applications to see if they work as expected and catch errors early in the development stage .

like image 39
itsundefined Avatar answered Nov 20 '22 04:11

itsundefined