-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
assert: add partialDeepStrictEqual #54630
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 partialDeepStrictEqual #54630
Conversation
466c2f0
to
c0a58e2
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #54630 +/- ##
==========================================
+ Coverage 88.23% 88.50% +0.26%
==========================================
Files 652 653 +1
Lines 183920 187937 +4017
Branches 35863 36233 +370
==========================================
+ Hits 162286 166333 +4047
+ Misses 14913 14827 -86
- Partials 6721 6777 +56
|
c0a58e2
to
04e92f0
Compare
The
notable-change
Please suggest a text for the release notes if you'd like to include a more detailed summary, then proceed to update the PR description with the text or a link to the notable change suggested text comment. Otherwise, the commit will be placed in the Other Notable Changes section. |
This PR adds new functionality, hence semver-minor This PR's new functionality (IMO) is notable-change |
04e92f0
to
0ee83d4
Compare
doc/api/assert.md
Outdated
// AssertionError | ||
``` | ||
|
||
## `assert.includes(actual, expected[, message])` |
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 less convinced that this one is useful. Why not just simply use assert(actual.includes(expected))
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 I just followed the most "approved" comment in the original PR and implemented it :)
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 agree with @jasnell and don't think this should be implemented. Let us concentrate on the partial inclusion. If anyone would ever ask for something else, we can still implement more.
is there any way we can push this forward? ar far as I know there is a little bit of people waiting for this and no clear consensus on the naming convention :) |
I still have concerns about whether we need everything in this PR but would very much like others to weigh in. @nodejs/test_runner @nodejs/assert ... anyone have thoughts? |
re naming, I definitely think the term "match" should be reserved for regexes. if the only difference between these new methods and deepEqual etc is that they allow extra properties, then perhaps it would make more sense as an option to the existing methods rather than entirely new ones? |
@ljharb the discussion where the "option" was discarded in favor of the new API started from this comment: #50399 (comment) |
other method names we could consider:
|
To clarify, the current proposal takes an "actual" and "expected", and it checks that every property on What about if I want to assert that a property is not present? is there a way to represent "never"? |
good questions! yes, this test will pass: {
description: 'compares two deeply nested objects with partial equality',
actual: { a: {nested: {property: true, some: 'other'}} },
expected: { a: {nested: {property: true}} },
}, and no, there is no way to check "not inclusion" , just if an object is a subset of another |
|
that would deviate from the already present implementations, like |
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.
implementation LGTM, I agree with the concerns on naming, lets change the name so this can land?
+1 for partialDeepEqual
Agreed. Also, should this have a |
ab7e68e
to
3d52d4b
Compare
Fixes: #50399
Took heavy inspiration from #53415 , trying to push it to the finish line 🚀
On top of it, I took the liberty of:
Notable change
Introducing experimental
assert.partialDeepStrictEqual
Sometimes, when writing test, we want to validate that some specific properties
are present, and the mere presence of additional keys are not exactly relevant
for that specific test. For this use case, we can now use
assert.partialDeepStrictEqual
, which should be familiar to those already usingassert.deepStrictEqual
, with the main difference that it does not require allproperties in the
actual
parameter to be present in theexpected
parameter.Here are a few examples of usage:
Co-Authored-By: Cristian Barlutiu