Asserts

An assertion is a boolean expression at a specific point in a program which will be true unless there is a bug in the program.

In simple words, it is check pint to check everything works fine or not, when assertion result is failed then that particular step will be considered as fail, when assertion result is pass, it does not have any effect.

Whether assertion pass/fails it does not halt the execution of the program.

We have to import the assert module to use the different assertion in nodejs var assert = require('assert');

Different assertion present in Nodejs

assert()

The assert() function tests if a given expression is true or not, If the expression evaluates to 0, or false, an assertion failure is being caused, and the program is terminated. Syntax for assert()


assert(expression, messageIfFailure);


expression : boolean value or expression which results in boolean value, if expression is true then assertion is passed otherwise assertion fails it.
messageIfFailure : FailureMessage will be printed or transferred to reports only when the assertion fails, this parameter is optional


var assert = require('assert');
assert(7 > 3, "7 is less than 3");

assert(true, "failed because expression resulted in false");

What is strict Comparison:

JavaScript has both strict and type–converting comparisons. A strict comparison (e.g., ===) is only true if the operands are of the same type and the contents match.

The more commonly-used abstract comparison (e.g. ==) converts the operands to the same type before making the comparison.

For relational abstract comparisons (e.g., <=), the operands are first converted to primitives, then to the same type, before comparison.

JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
  • Two Boolean operands are strictly equal if both are true or both are false.
  • Two objects are strictly equal if they refer to the same Object.
  • Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]

0 == false   // true
0 === false  // false, because they are of a different type
1 == "1"     // true, automatic type conversion for value only
1 === "1"    // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false

deepStrictEqual()

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


deepStrictEqual(actual, expected[, message])


actual : Required. Specifies the first value to be compared
expected : Required. Specifies the second value to be compared
message : Optional. Specifies the error message to be assigned to the AssertionError. If omitted, a default message is assigned
If the two objects are not equal, an assertion failure is being caused, and the program is terminated.


var assert = require('assert');
assert.deepStrictEqual(1, 1[, " message if fails])

doesNotThrow()

doesNotThrow() methods checks whether given function does not throw any Error/Exception, doesNotThrow() accepts function to execute and a error to throw if the function throws any error. When assert.doesNotThrow() is called, it will immediately call the fn function.


assert.doesNotThrow(fn[, error][, message])


var assert = require('assert');
assert.doesNotThrow(
  function() {
    console.log("Nothing to see here");
  },
  Error, "Error is thrown"
);

equal()

The equal() 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.


assert.equal(actual, expected[, message])


var assert = require('assert');
assert.equal("karthiq", "karthiq", "error message")

fail()

Sometimes you might have a condition, where you should compare the values in if..else block but based on the result you might want to fail the assertion without needing any inputs, in such cases you can go for fail() function from assertion module in nodejs


var assert = require('assert');
assert.fail("error message")

ifError()

ifError() function throws error if the given value evaluates to error or any value(except zero), or to true


var assert = require('assert');

assert.ifError(0); // no error

assert.ifError(1); // throws error

assert.ifError(false); // no error

assert.ifError(true); // throws error

assert.ifError('error'); // throws error

notDeepStrictEqual()

assert.notDeepStrictEqual() function tests if two objects, and their child objects, are NOT equal, using the !== operator. An assertion failure is being caused if both objects are equal


notDeepStrictEqual(actual, expected[, message])


assert.notDeepStrictEqual({ a: 1 }, { a: '1' });

notEqual() [depreciated]

assert.notEqual() method tests if two values are NOT equal, using the != operator. Tests shallow, coercive inequality with the Abstract Equality Comparison ( != ).


const assert = require('assert');

assert.notEqual(1, 2);
// OK

assert.notEqual(1, 1);
// AssertionError: 1 != 1

assert.notEqual(1, '1');
// AssertionError: 1 != '1'

notStrictEqual()

If the values are strictly equal (===), an AssertionError is thrown with a message property set equal to the value of the message parameter.

If the message parameter is undefined, a default error message is assigned. If the message parameter is an instance of an Error then it will be thrown instead of the AssertionError.


const assert = require('assert');

assert.notStrictEqual(1, 2);
// OK

assert.notStrictEqual(1, 1);
// AssertionError [ERR_ASSERTION]: Identical input passed to notStrictEqual: 1

assert.notStrictEqual(1, '1');
// OK

ok()

assert.ok() method tests if a given expression is true or not, If the expression evaluates to 0, or false, an AssertionError is being caused, and the program is terminated in nodejs.


var assert = require('assert');
assert.ok(5 > 7, "error goes here");

strictEqual()

The assert.strictEqual() method tests if two values are equal, using the === operator. === operator tests if the values are equal and if their types are equal.


const assert = require('assert').strict;

assert.strictEqual(1, 2);
// AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B:
// + expected - actual
// - 1
// + 2

assert.strictEqual(1, 1);
// OK

assert.strictEqual(1, '1');
// AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B:
// + expected - actual
// - 1
// + '1'

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to chercher.tech@gmail.com

or Register below


 
Join My Facebook Group
Join Group