-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
deepStrictEqual comparison output is unhelpful on prototype-only differences #22141
Comments
@damianobarbati in your test, their prototypes differ. The object that is returned from the regexp in A simplified version of your testcase: const assert = require('assert').strict;
function split(pathname) {
const regexp = new RegExp("^/(?<id>\\d+?)$", 'i');
const matches = pathname.match(regexp);
return matches.groups;
}
try {
assert.deepStrictEqual(
split('/123', '/:id'),
{ id: '123' },
'with prototype'
);
} catch (e) {
console.error(e);
}
try {
assert.deepStrictEqual(
split('/123', '/:id'),
Object.assign(Object.create(null), { id: '123' }),
'without prototype'
);
} catch (e) {
console.error(e);
} Note that only the first of those two tests fails. |
That is documented: https://nodejs.org/api/assert.html#assert_comparison_details Moreover, that is what the main difference between |
Replacing |
Oh I see now, The 2 misleading factors:
Thanks @ChALkeR! |
@damianobarbati No, |
$ node
> const x = {}; x.id = '123'; x
{ id: '123' }
> const y = Object.create(null); y.id = '123'; y
{}
> assert.strict.deepEqual(x, y)
AssertionError [ERR_ASSERTION]: Input objects not identical:
{}
> Object.getPrototypeOf(x)
{}
> Object.getPrototypeOf(y)
null
> x.toString()
'[object Object]'
> y.toString()
TypeError: y.toString is not a function
|
As far as I understand the main issue it is not about the comparison details but about the output and that is indeed not ideal. The main difficulty here is how to visualize the difference between such objects. I struggled with this as well and I would like to fix this. I just could not yet come up with a great solution so far. I am going to look into improving that though. |
The docs list all the comparison details. See https://nodejs.org/api/assert.html#assert_assert_deepstrictequal_actual_expected_message.
Those values contain the actual input values and what you see is the inspected value as a string. However, the output is only as "good" or "bad" as the inspection tool you use. |
Ok, that makes sense. I renamed the issue then. |
Hey @BridgeAR @ChALkeR , thought of giving a crack at this. I was seeing through the source code, and I found the issue is we are not showing up the error message properly when two objects are compared and one of those object's The fix should be straight forward , if we have a object whose prototype is
The
which seems good to me -- clearly indicates prototype is not equal. Well, of course, if we do these changes, we need to work on Let me know your thoughts on this. Thanks! |
@antsmartian I think it is actually a good idea to change the output in If the |
Yes, as I have shown above.
Yes, using
I will work on this and raise a PR. Note: You have actually mentioned the wrong person in your reply , instead of me ;) |
This makes sure the prototype is always detected properly. PR-URL: nodejs#22331 Fixes: nodejs#22141 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
This makes sure the prototype is always detected properly. Backport-PR-URL: #23655 PR-URL: #22331 Fixes: #22141 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
This makes sure the prototype is always detected properly. Backport-PR-URL: #23655 PR-URL: #22331 Fixes: #22141 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
This makes sure the prototype is always detected properly. Backport-PR-URL: #23655 PR-URL: #22331 Fixes: #22141 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Throws a misterious:
What does this mean? Where's the difference? 🤨
The text was updated successfully, but these errors were encountered: