In Chai assertion library what is the use of "assert.equal()" when we already have "assert.deepEqual()" and "assert.strictEqual()" for both strict and deep equality assertions? Also it is mentioned that "assert.equal()" uses non-strict equality. What is the difference between non-strict and deep equality? Why can't we just use "deepEqual" instead of "equal"?
In Chai assertion library what is the use of "assert.equal()" when we already have "assert.deepEqual()" and "assert.strictEqual()" for both strict and deep equality assertions? Also it is mentioned that "assert.equal()" uses non-strict equality. What is the difference between non-strict and deep equality? Why can't we just use "deepEqual" instead of "equal"?
Share Improve this question asked Aug 4, 2020 at 15:11 Pranjal SrivastavaPranjal Srivastava 611 silver badge2 bronze badges2 Answers
Reset to default 4When you check out the Chai documentation for the .deep
functions, you'll get a pretty clear description:
Causes all .equal, .include, .members, .keys, and .property assertions that follow in the chain to use deep equality instead of strict (===) equality.
When you use equal
, Chai uses a ===
parision. So when paring objects, Chai will check for reference.
When you use deepEqual
, Chai will go down the object hierarchy and pare every value of every property.
Example:
const a = {"a": "a"};
const b = {"a": "a"};
expect(a).to.equal(b); // false, as a refers to a different object than b
expect(a).to.deep.equal(b); // true, as the value of every property of a and b equals
https://www.chaijs./api/assert/
Javascript as 3 parisons mainly ,
== , === and Object.is(obj1,obj2)
- double equals (
==
) will perform a type conversion when paring two things, and will handleNaN
,-0
, and+0
specially to conform to IEEE 754 (soNaN != NaN
, and-0 == +0
);- triple equals (
===
) will do the same parison as double equals (including the special handling forNaN
,-0
, and+0
) but without type conversion; if the types differ,false
is returned.Object.is
does no type conversion and no special handling forNaN
,-0
, and+0
(giving it the same behavior as===
except on those special numeric values).
https://developer.mozilla/en-US/docs/Web/JavaScript/Equality_parisons_and_sameness#a_model_for_understanding_equality_parisons
But all these parisons works only on primitive values (non objects like : string, number, bigint, boolean, undefined, symbol, and null .)
For Object , it will consider the reference also , meaning it will check whether the objects have same reference or not else it will equate to false. Hence we cannot use any of the three methods for paring Objects
assert.equal does '==' assert.strict does '===' assert.deep recursively check key, value and members instead of reference (so deep content validation)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744151710a4560673.html
评论列表(0条)