FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

assert: add `matchObjectStrict` and `matchObject` by synapse · Pull Request #53415 · nodejs/node · GitHub

/ node Public

assert: add matchObjectStrict and matchObject - #53415

Closed
synapse wants to merge 1 commit into
nodejs:mainfrom
synapse:main
Closed

assert: add matchObjectStrict and matchObject#53415
synapse wants to merge 1 commit into
nodejs:mainfrom
synapse:main

Conversation

synapse commented Jun 11, 2024
edited
Loading

Copy link
Copy Markdown
Contributor

fixes #50399

What is this PR doing:

  • Added 2 new assert functions matchObjectStrict & matchObject
  • Added tests to check different object types (Feel free to suggest more types to be tested)
  • Added a new section in the Assert documentation to explain the usage

The compareBranch (used by matchObject) works as follows:

  • matchObject recursively traverses the actual objects, Map, and Set and rely on deepEqual to compare its values (event across different realms) - N.B. array are directly compared using deepEqual and will not match if they are from different realms
  • if the value is a plain object then it will retrigger the compareBranch from that branch onward
  • else will compare the actual and expected values using the built-in isDeepEqual or isDeepStrictEqual based on the loose/strict param
  • it compares keys - (using Reflect.ownKeys() instead of Object.keys() to include symbol properties)
  • checks for recursive object values

nodejs-github-bot added assert Issues and PRs related to the assert subsystem. needs-ci PRs that need a full CI run. labels Jun 11, 2024

aduh95 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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).

Comment thread test/parallel/test-assert-objects.js Outdated
Comment thread doc/api/assert.md Outdated
- `expected` {any}
- `message` {string|Error}

Tests strict equality between the `actual` and `expected` parameters.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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?

Comment thread doc/api/assert.md Outdated
Comment on lines +2579 to +2580
- `actual` {any}
- `expected` {any}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is it actually any? What happens if we pass a primitive?

synapse Jun 11, 2024
edited
Loading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Comment thread doc/api/assert.md Outdated
Comment thread doc/api/assert.md Outdated
aduh95 changed the title lib: Added assert.matchObjectStrict & assert.matchObject assert: add matchObjectStrict and matchObject Jun 11, 2024
Comment thread doc/api/assert.md Outdated
Comment thread lib/assert.js Outdated
Comment thread lib/assert.js Outdated
Comment thread lib/assert.js Outdated
Comment thread test/parallel/test-assert-objects.js Outdated
Comment thread test/parallel/test-assert-objects.js Outdated
Comment thread test/parallel/test-assert-objects.js Outdated

targos commented Jun 11, 2024

Copy link
Copy Markdown
Member

@nodejs/assert

panva left a comment
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread doc/api/assert.md Outdated

panva commented Jun 11, 2024
edited
Loading

Copy link
Copy Markdown
Member

Based on the docs I find it very confusing to discern between this and assert.deep(Strict)Equal. "Matching" suggests (to me at least) that the "expected" argument might be an object with more properties than "actual" but that's not what's documented nor implemented.

Comment thread lib/assert.js Outdated

synapse commented Jun 11, 2024

Copy link
Copy Markdown
Contributor Author

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

Should be handled now

Comment thread lib/assert.js Outdated
Comment thread lib/assert.js Outdated
Comment thread lib/assert.js Outdated
Comment thread lib/assert.js Outdated
Comment thread lib/assert.js Outdated
Comment thread lib/assert.js Outdated
Comment thread lib/assert.js Outdated
Comment thread doc/api/assert.md Outdated
Comment thread doc/api/assert.md Outdated
synapse force-pushed the main branch 2 times, most recently from 9b2b89c to c7e9950 Compare June 13, 2024 14:18
Comment thread test/parallel/test-assert-objects.js Outdated
Comment thread test/parallel/test-assert-objects.js Outdated
Comment thread test/parallel/test-assert-objects.js Outdated
Comment thread test/parallel/test-assert-objects.js Outdated
Comment thread test/parallel/test-assert-objects.js Outdated

MoLow commented Jun 14, 2024

Copy link
Copy Markdown
Member

please remove merge commit. changes generally LGTM, I think this is a great addition to node:assert

Comment thread doc/api/assert.md Outdated
Trott added the commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. label Jun 16, 2024
Trott force-pushed the main branch 2 times, most recently from d977fd6 to 6921e96 Compare June 16, 2024 22:02
Trott added the semver-minor PRs that contain new features and should be released in the next minor version. label Jun 16, 2024

Trott commented Jun 16, 2024

Copy link
Copy Markdown
Member

@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?

Comment thread doc/api/assert.md Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

There was some conversation in the original issue about whether it should be:

  1. a new method, e.g. matchObject
  2. an option to the existing method, e.g. deepStrictEqual(..., { partial: tru })
  3. 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

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 <duhamelantoine1995@gmail.com>
Comment thread doc/api/assert.md
Comment on lines +2619 to +2620
present in the `actual` parameter with equivalent values, permitting type coercion
where necessary.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

these seem flipped - the strict variant should not allow type coercion, and the loose variant should permit it.

Copy link
Copy Markdown
Contributor

@synapse See #50399 (comment).

simoneb commented Sep 13, 2024

Copy link
Copy Markdown
Contributor

This PR can be closed as work on the linked issue is being done elsewhere.

Copy link
Copy Markdown
Contributor

This PR can be closed as work on the linked issue is being done elsewhere.

Where is that work happening?

simoneb commented Sep 23, 2024

Copy link
Copy Markdown
Contributor

This PR can be closed as work on the linked issue is being done elsewhere.

Where is that work happening?

#54630

avivkeller added the stalled Issues and PRs that are stalled. label Sep 23, 2024

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Member

@synapse are you still interested in this PR, given that it is possibly superceded by #54630?

synapse commented Sep 23, 2024

Copy link
Copy Markdown
Contributor Author

@synapse are you still interested in this PR, given that it is possibly superceded by #54630?

No, I will close this off

synapse closed this Sep 23, 2024

Copy link
Copy Markdown
Contributor

This PR can be closed as work on the linked issue is being done elsewhere.

Where is that work happening?

#54630

Thank you.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

assert Issues and PRs related to the assert subsystem. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version. stalled Issues and PRs that are stalled.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: assert.matchObject

Back | FazBrowse Home | New Git URL