Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between equal and eql in Chai Library

I'm pretty new to JavaScript, and I have a quick question regarding the Chai library for making unit tests.

When I was studying some materials on the Chai library, I saw a statement saying:

  • equal: Asserts that the target is strictly (===) equal to the given value.
  • eql: Asserts that the target is deeply equal to value.

I'm confused about what the difference is between strictly and deeply.

like image 712
chanwcom Avatar asked Apr 22 '16 16:04

chanwcom


People also ask

What is the difference between EQL and equal in Javascript?

equal() vs eql()equal() asserts that two arguments are referentially equal (ie. a === b) . eql() does a deep equality check between two arguments.

What is the difference between expect and assert in Chai?

ASSERT: Fails fast, aborting the current function. EXPECT: Continues after the failure.

What is Chai assertion?

Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

Do you need assertion in Chai?

The should style allows for the same chainable assertions as the expect interface, however it extends each object with a should property to start your chain. This style has some issues when used with Internet Explorer, so be aware of browser compatibility.


1 Answers

Strictly equal (or ===) means that your are comparing exactly the same object to itself:

var myObj = {    testProperty: 'testValue' }; var anotherReference = myObj;  expect(myObj).to.equal(anotherReference); // The same object, only referenced by another variable expect(myObj).to.not.equal({testProperty: 'testValue'}); // Even though it has the same property and value, it is not exactly the same object 

Deeply Equal on the other hand means that every property of the compared objects (and possible deep linked objects) have the same value. So:

var myObject = {     testProperty: 'testValue',     deepObj: {         deepTestProperty: 'deepTestValue'     } } var anotherObject = {     testProperty: 'testValue',     deepObj: {         deepTestProperty: 'deepTestValue'     } } var myOtherReference = myObject;  expect(myObject).to.eql(anotherObject); // is true as all properties are the same, even the inner object (deep) one expect(myObject).to.eql(myOtherReference) // is still also true for the same reason 
like image 74
David Losert Avatar answered Sep 28 '22 00:09

David Losert