Table of contents
1.
Introduction
2.
Installation
3.
Deep-Eql Examples
4.
Usage
4.1.
Rules
4.1.1.
Program:
4.1.2.
Output:
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Deep-eql

Author Aman Thakur
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article we will look into the scenario of why do we even require a deep-eql of chai module?  Strictly equal (or ===) means that you are comparing exactly the same object to itself whereas deep-eql on the other hand means that every property of the compared objects (and possibly deep linked objects) have the same value.

The article covers the implementation of a package named deep-eql, its example, installation, and usage. deep-eql is a module that checks an object's keys recursively until it discovers primitives to check for referential equality, rather than having referential equality (a === b).

I think there's enough of an intro and Let’s see a few examples and usage of deep-eql .

Installation

First of all before getting started with examples first install the required packages of your choice using package manager such as npm or yarn.

Using npm,

npm install deep-eql
You can also try this code with Online Javascript Compiler
Run Code

Or using yarn,

yarn add deep-eql
You can also try this code with Online Javascript Compiler
Run Code

Deep-Eql Examples

1. These are primitives, and they all have the same reference - they're all the same.

Here 1 == ‘1’ is true because they are having the same value. However 1 === ‘1’ is false as character and integer are different internally.

 

2. These are two separate primitives which have the same value due to type coercion - they are roughly equivalent.

console.log(`1 == 1`, 1 == '1');

 

3. These are two separate objects with different references, so they aren't technically equal despite having the identical data inside.

console.log(`{ a: 1 } !== { a: 1 }`, { a: 1 } !== { a: 1 });

 

4. Because they are of the same type, loose equality conducts the same check as strict equality: they are not equal.

{ a: 1 } != { a: 1 }

 

5. deepEql can tell that they have the same keys and the same values for those keys, hence they are deeply equivalent!

var deepEql = require("deep-eql");
console.log(`deepEql({ a: 1 }, { a: 1 }) === true`, deepEql({ a: 1 }, { a: 1 }) === true);

Usage

The primary export of deep-eql is a function that can be given two objects to compare.It will always produce a boolean value that may be used to compare two objects to see if they are profoundly equal.

Rules

1. According to Object.is, strict equality for non-traversable nodes:

eql(NaN, NaN).should.be.true;
eql(-0, +0).should.be.false;

 

2. All enumerable properties, both owned and inherited, are considered:

eql(Object.create({ f: { a: 1 } }), Object.create({ f: { a: 1 } })).should.be
.true;
eql(Object.create({ f: { a: 1 } }), Object.create({ f: { a: 2 } })).should.be
.false;

 

3. Only the name, message, and code attributes of Error objects are compared, independent of enumerability:

eql(Error("coding"), Error("ninjas")).should.be.true;
eql(Error("coding"), Error("ninjas")).should.be.false;
eql(Error("coding"), TypeError("coding")).should.be.false;
eql(
Object.assign(Error("coding"), { code: 42 }),
Object.assign(Error("coding"), { code: 42 })
).should.be.true;
eql(
Object.assign(Error("coding"), { code: 42 }),
Object.assign(Error("coding"), { code: 13 })
).should.be.false;
eql(
Object.assign(Error("coding"), { otherProp: 42 }),
Object.assign(Error("coding"), { otherProp: 13 })
).should.be.true;

 

4. Arrays aren't used for arguments:

eql([], arguments).should.be.false;
eql([], Array.prototype.slice.call(arguments)).should.be.true;

Program:

const deepEql = require("deep-eql");
const { should } = require("chai").should();

describe('Deep-Eql test suite!!', function(){
   it('Strict equality for non-traversable', function() {
       console.log(deepEql(NaN, NaN).should.be.true);
       console.log(deepEql(-0, +0).should.be.false);
   });

   it('All own and inherited enumerable properties are considered', function(){
       console.log(deepEql(Object.create({ foo: { a: 1 } }), Object.create({ foo: { a: 1 } })).should.be.true);
       console.log(deepEql(Object.create({ foo: { a: 1 } }), Object.create({ foo: { a: 2 } })).should.be.false);
   });

   it('When comparing Error objects, only name, message, and code properties are considered, regardless of enumerability', function (){
       console.log(deepEql(Error('coding'), Error('coding')).should.be.true);
       console.log(deepEql(Error('coding'), Error('ninjas')).should.be.false);
       console.log(deepEql(Error('coding'), TypeError('coding')).should.be.false);
   });

   it('Arguments are not Arrays', function (){
       console.log(deepEql([], arguments).should.be.false);
       console.log(deepEql([], Array.prototype.slice.call(arguments)).should.be.true);
   });
});
You can also try this code with Online Javascript Compiler
Run Code

Output:

FAQs

1. What assertion styles are present in Chai testing assertion library?
Ans: Chai is an assertion library that offers certain interfaces for any JavaScript-based framework to implement assertions. TDD styles and BDD styles are Chai's two types of interfaces.

 

2. Can you tell me if Mocha.js is a BDD tool?
Ans: Mocha.js' default interface is BDD. Aside from that, Mocha.js has a number of APIs for defining test suites, hooks, and individual tests. TSS, Exports, QUnit, and Require are examples of these.


3. What are Mocha's CLI options?
Ans: When you use the Mocha CLI binaries, you have access to a number of tools. The list of all accessible options in the command line, combined with a brief explanation of how they are implemented, is rather straightforward to find. This is how you get to the list:  mocha -h

 

4. What does mocha describe()?
Ans: The describe() function gives a way to group your tests in Mocha.js. You can nest your tests in groups as deep as you require. The describe() function takes two arguments: A name of test groups and A callback function

Key Takeaways

If you have reached till here, that means you find this article really helpful; the deep-eql is a utility feature provided in the chai module and helps us test the referential equality of the object recursively.

You might be interested in articles such as Fundamentals of Software Testing. Hence never stop your quest for learning. Do give an upvote to the article if you enjoyed it. We wish you Good Luck! Keep coding and keep reading Ninja. 

Happy Learning

Live masterclass