-
Notifications
You must be signed in to change notification settings - Fork 29.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
assert: add matchObjectStrict
and matchObject
#53415
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a lot of unrelated changes, making this hard to review. Consider removing those (maybe a problem with the configuration of your text editor?), and run the linters (NODE=node make test-doc -j
and make lint-js
).
doc/api/assert.md
Outdated
- `expected` {any} | ||
- `message` {string|Error} | ||
|
||
Tests strict equality between the `actual` and `expected` parameters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's hardly what's happening, is it? At least I find the wording very confusing (I would expect the same behavior as strictEqual
with the current wording), we should explain in more details what's happening here, what's being compared: is it only own properties? is the prototype important?
doc/api/assert.md
Outdated
- `actual` {any} | ||
- `expected` {any} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it actually any? What happens if we pass a primitive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's any because it compares the values (and the keys), which could be primitives. I'll add a couple of tests for primitives too.
assert.matchObjectStrict
& assert.matchObject
matchObjectStrict
and matchObject
@nodejs/assert |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to suggest more types to be tested.
Should't this fall back on assert.deepEqual
and assert.deepStrictEqual
respectively when dealing with anything but simple objects?
If I pass two different KeyObject or CryptoKey instances to matchObject
, despite them being different cryptographic keys this doens't throw.
If it falled back on on assert.deepEqual
and assert.deepStrictEqual
then handling of those respective objects would be handled (#50897).
lib/internal/util/comparisons.js
already has these already in
Based on the docs I find it very confusing to discern between this and |
Should be handled now |
9b2b89c
to
c7e9950
Compare
please remove merge commit. changes generally LGTM, I think this is a great addition to |
d977fd6
to
6921e96
Compare
@nodejs/assert How sure are we that we want to add this? If it lands, should it be added as experimental for a while to give us the option to change it as we see fit for a while or even remove it entirely without waiting for a major release? |
// OK | ||
|
||
assert.matchObject({ a: 1, b: 2, c: 3 }, { a: 1, b: 2 }); | ||
// OK |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still not convinced that this is sufficiently different from deepEqual
and deepStrictEqual
to warrant a new API. At the absolute least the documentation does not provide enough information for someone to be able to decide which to use
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasnell If the expected
object has fewer properties than the actual
object, deepEqual
will throw an error based on your example. On the other hand, matchObject
will compare the existing properties and values in the expected
object, allowing for a partial comparison.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I get that, I just do not think there's enough justification for a new api. A new option to the existing method could do the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was some conversation in the original issue about whether it should be:
- a new method, e.g. matchObject
- an option to the existing method, e.g. deepStrictEqual(..., { partial: tru })
- a utility function for the existing method, e.g. deepStrictEqual(..., objectContaining(...))
here @synapse decided to go with option 1, lacking a clear consensus on which option made the most sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still not convinced that this is sufficiently different from
deepEqual
anddeepStrictEqual
to warrant a new API. At the absolute least the documentation does not provide enough information for someone to be able to decide which to use
I agree. I would love to see node:assert
have a match
function equivalent to https://github.com/tapjs/tapjs/blob/8f2baa7eb5e1f4cd0fcf8b60da4fb4e41ea680f1/docs/src/content/docs/api/asserts/index.md?plain=1#L318-L338
But the proposal here seems like a less consistent deepStrictEqual
.
Co-authored-by: Antoine du Hamel <[email protected]>
present in the `actual` parameter with equivalent values, permitting type coercion | ||
where necessary. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these seem flipped - the strict variant should not allow type coercion, and the loose variant should permit it.
@synapse See #50399 (comment). |
This PR can be closed as work on the linked issue is being done elsewhere. |
Where is that work happening? |
|
This issue/PR was marked as stalled, it will be automatically closed in 30 days. If it should remain open, please leave a comment explaining why it should remain open. |
Thank you. |
fixes #50399
What is this PR doing:
matchObjectStrict
&matchObject
The
compareBranch
(used bymatchObject
) works as follows:matchObject
recursively traverses theactual
objects
,Map
, andSet
and rely ondeepEqual
to compare its values (event across different realms) - N.B.array
are directly compared usingdeepEqual
and will not match if they are from different realmsif
the value is a plain object then it will retrigger thecompareBranch
from that branch onwardelse
will compare theactual
andexpected
values using the built-inisDeepEqual
orisDeepStrictEqual
based on theloose/strict
paramReflect.ownKeys()
instead ofObject.keys()
to include symbol properties)