-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
assert: add matchObjectStrict
and matchObject
#53415
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2539,6 +2539,130 @@ assert.throws(throwingFirst, /Second$/); | |
// AssertionError [ERR_ASSERTION] | ||
``` | ||
|
||
## `assert.matchObject(actual, expected[, message])` | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
* `actual` {any} | ||
* `expected` {any} | ||
* `message` {string|Error} | ||
|
||
Evaluates the equivalence between the `actual` and `expected` parameters by | ||
performing a deep comparison. This function ensures that all properties defined | ||
in the `expected` parameter exactly match those in the `actual` parameter in | ||
both value and type, without allowing type coercion. | ||
|
||
```mjs | ||
import assert from 'node:assert'; | ||
|
||
assert.matchObject({ a: 1, b: '2' }, { a: 1, b: 2 }); | ||
// OK | ||
synapse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
assert.matchObject({ a: 1, b: '2', c: 3 }, { a: 1, b: 2 }); | ||
// OK | ||
|
||
assert.matchObject({ a: { b: { c: '1' } } }, { a: { b: { c: 1 } } }); | ||
// OK | ||
|
||
assert.matchObject({ a: 1 }, { a: 1, b: 2 }); | ||
// AssertionError | ||
|
||
assert.matchObject({ a: 1, b: true }, { a: 1, b: 'true' }); | ||
// AssertionError | ||
|
||
assert.matchObject({ a: { b: 2 } }, { a: { b: 2, c: 3 } }); | ||
// AssertionError | ||
``` | ||
|
||
```cjs | ||
const assert = require('node:assert'); | ||
|
||
assert.matchObject({ a: 1, b: '2' }, { a: 1, b: 2 }); | ||
// OK | ||
|
||
assert.matchObject({ a: 1, b: '2', c: 3 }, { a: 1, b: 2 }); | ||
// OK | ||
|
||
assert.matchObject({ a: { b: { c: '1' } } }, { a: { b: { c: 1 } } }); | ||
// OK | ||
|
||
assert.matchObject({ a: 1 }, { a: 1, b: 2 }); | ||
// AssertionError: Expected key b | ||
|
||
assert.matchObject({ a: 1, b: true }, { a: 1, b: 'true' }); | ||
// AssertionError | ||
|
||
assert.matchObject({ a: { b: 2, d: 4 } }, { a: { b: 2, c: 3 } }); | ||
// AssertionError: Expected key c | ||
``` | ||
|
||
If the values or keys are not equal in the `expected` parameter, an [`AssertionError`][] is thrown with a `message` | ||
property set equal to the value of the `message` parameter. If the `message` | ||
parameter is undefined, a default error message is assigned. If the `message` | ||
parameter is an instance of an [`Error`][] then it will be thrown instead of the | ||
`AssertionError`. | ||
|
||
## `assert.matchObjectStrict(actual, expected[, message])` | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
* `actual` {any} | ||
* `expected` {any} | ||
* `message` {string|Error} | ||
|
||
Assesses the equivalence between the `actual` and `expected` parameters through a | ||
deep comparison, ensuring that all properties in the `expected` parameter are | ||
present in the `actual` parameter with equivalent values, permitting type coercion | ||
where necessary. | ||
Comment on lines
+2619
to
+2620
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
```mjs | ||
import assert from 'node:assert'; | ||
|
||
assert.matchObject({ a: 1, b: 2 }, { a: 1, b: 2 }); | ||
// OK | ||
|
||
assert.matchObject({ a: { b: { c: 1 } } }, { a: { b: { c: 1 } } }); | ||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more. I'm still not convinced that this is sufficiently different from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jasnell If the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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:
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 commentThe reason will be displayed to describe this comment to others. Learn more.
I agree. I would love to see But the proposal here seems like a less consistent |
||
|
||
assert.matchObject({ a: 1 }, { a: 1, b: 2 }); | ||
// AssertionError | ||
|
||
assert.matchObject({ a: 1, b: '2' }, { a: 1, b: 2 }); | ||
// AssertionError | ||
|
||
assert.matchObject({ a: { b: 2 } }, { a: { b: '2' } }); | ||
// AssertionError | ||
``` | ||
|
||
```cjs | ||
const assert = require('node:assert'); | ||
|
||
assert.matchObject({ a: 1, b: 2 }, { a: 1, b: 2 }); | ||
// OK | ||
|
||
assert.matchObject({ a: { b: { c: 1 } } }, { a: { b: { c: 1 } } }); | ||
// OK | ||
|
||
assert.matchObject({ a: 1, b: 2, c: 3 }, { a: 1, b: 2 }); | ||
// OK | ||
|
||
assert.matchObject({ a: 1 }, { a: 1, b: 2 }); | ||
// AssertionError | ||
|
||
assert.matchObject({ a: 1, b: '2' }, { a: 1, b: 2 }); | ||
// AssertionError | ||
|
||
assert.matchObject({ a: { b: 2 } }, { a: { b: '2' } }); | ||
// AssertionError | ||
``` | ||
|
||
Due to the confusing error-prone notation, avoid a string as the second | ||
argument. | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.