Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

Assert module in nodeJS

Introduction
  1. The assert module provides a set of assertion functions for verifying invariants
  2. We will learn the most used assertion functions
Common assertion functions
  1. assert.deepEqual()
  2. assert.equal()
  3. assert.notDeepEqual()
  4. assert.notEqual()
assert.deepEqual()
Tests for deep equality between the actual and expected parameters
Comparison details
  1. Primitive values are compared with the Abstract Equality Comparison ( == ) with the exception of NaN.
  2. It is treated as being identical in case both sides are NaN.
  3. Type tags of objects should be the same.
  4. Only enumerable own properties are considered.
  5. Error names and messages are always compared, even if these are not enumerable properties
  6. Object wrappers are compared both as objects and unwrapped values.
  7. Object properties are compared unordered.
  8. Map keys and Set items are compared unordered.
  9. Recursion stops when both sides differ or both sides encounter a circular reference
  10. Implementation does not test the [[Prototype]] of objects.
  11. Symbol properties are not compared
  12. WeakMap and WeakSet comparison does not rely on their values
    const assert = require('assert')
console.log(assert.deepEqual({ a: 1 }, { a: 1 }));
// OK
console.log(assert.deepEqual({ a: 1 }, { a: 2 }));
// ERR_ASSERTION
assert.equal()
Tests shallow, coercive equality between the actual and expected parameters using the Abstract Equality Comparison ( == )
  const assert = require('assert')

assert.equal(1, 1);
// OK
assert.equal(1, 2);
// ERR_ASSERTION
assert.notDeepEqual()
Tests for any deep inequality. Opposite of assert.deepEqual().
const assert = require('assert')
assert.notDeepEqual({ a: 1 }, { a: 1 });
// ERR_ASSERTION
assert.notDeepEqual({ a: 1 }, { a: 2 });
// OK
assert.notEqual()
Tests shallow, coercive inequality with the Abstract Equality Comparison (!= ).
const assert = require('assert')
assert.notEqual(1, 1);
// ERR_ASSERTION
assert.notEqual(1, 2);
//  OK