Add an option for strict deepEqual (order of properties important)
See original GitHub issueDescription
The ECMAScript 6 specification defines in which order the properties of an object should be traversed (See: http://www.2ality.com/2015/10/property-traversal-order-es6.html).
Therefore, in ES6, the following ought to be false:
const result = {
stderr: '',
stdout: 'Hello, World!\n',
exitCode: 0
};
const expectedResult = {
exitCode: 0,
stderr: '',
stdout: 'Hello, World!\n'
};
t.deepEqual(result, expectedResult);
The current (ava@0.18.1) behaviour is that deepEqual
ignores the order of properties.
I suggest that deepEqual
would perform strict (order aware) deepEqual by default and ask user to sort order properties of result object if property order is not important for the test.
Relevant Links
- Related discussion on ESLint (https://github.com/eslint/eslint/issues/7714)
- Previous discussion where this suggestion has been declined on the (false) grounds that order is not important (https://github.com/avajs/ava/issues/899)
Environment
Tell us which operating system you are using, as well as which versions of Node.js, npm, and AVA. Run the following to get it quickly:
Node.js v7.5.0
darwin 16.3.0
0.18.1
4.1.2
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
5 Different Ways to Deep Compare JavaScript Objects
Deep equality: Determines whether objects are equal by comparing each property in the operands. Referential equality can be determined with ...
Read more >object - Javascript - deepEqual Comparison - Stack Overflow
Write a function, deepEqual, that takes two values and returns true only if they are the same value or are objects with the...
Read more >Testing Arrays and Objects with Chai.js | by Titus Stone
Important Concept: By default, all assertions in Chai are performing a strict equality comparison. Thus, asserting that an array of objects has ...
Read more >Object graph comparison - Fluent Assertions
Imagine you want to compare an Order and an OrderDto using BeEquivalentTo , but the first type has a Name property and the...
Read more >Assert | Node.js v19.3.0 Documentation
deepStrictEqual () instead. assert.deepEqual() can have surprising results. Deep equality means that the enumerable "own" properties of child objects are also ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I agree with @novemberborn and @vadimdemedes. I’m starting to understand why the order can be important, but I think this only matters in pretty rare cases. I would prefer this to be implemented in a new package.
I wouldn’t want
t.deepEqual
to check for the order of keys. As fort.keyOrder
, this feels like a “nice-to-have” feature, which generally we are against from. If someone absolutely has to check for the order of keys:t.deepEqual(Object.keys(obj))
.