What is the difference between equal and eql in Chai Library

好久不见. 提交于 2019-12-03 23:39:34

问题


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 value" and eql "Asserts that the target is deeply equal to value.".

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


回答1:


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


来源:https://stackoverflow.com/questions/36798993/what-is-the-difference-between-equal-and-eql-in-chai-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!