-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Comparing errors through deepEqual #3122
Comments
This looks like it's caused by code in |
Possible fix: #3124 |
Error objects have non-enumerable properties. So, unexpectedly, assert.deepEqual(new Error('a'), new Error('b')) will not throw an AssertionError. This commit changes that behavior. Fixes: nodejs#3122
Assert is now locked. Userland alternatives should be used. Assert is for testing Node.js itself. Document potentially surprising use of enumerable properties only in deep equality assertions. Ref: nodejs#3124 Ref: nodejs#3122 PR-URL: nodejs#3330 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
I'm afraid a fix for this is unlikely to land in Node.js. The API is now locked and users are encouraged to employ userland assertion libraries instead. If you don't want to use a userland assertion library for whatever reason, a workaround for your case might look like this:
Or, more thoroughly (like, if you want the stack traces to match exactly, which you typically probably don't):
|
Assert is now locked. Userland alternatives should be used. Assert is for testing Node.js itself. Document potentially surprising use of enumerable properties only in deep equality assertions. Ref: #3124 Ref: #3122 PR-URL: #3330 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
Assert is now locked. Userland alternatives should be used. Assert is for testing Node.js itself. Document potentially surprising use of enumerable properties only in deep equality assertions. Ref: #3124 Ref: #3122 PR-URL: #3330 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
Assert is now locked. Userland alternatives should be used. Assert is for testing Node.js itself. Document potentially surprising use of enumerable properties only in deep equality assertions. Ref: #3124 Ref: #3122 PR-URL: #3330 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
I'm implementing tests for my js project and I need to compare an error returned from a rejected promise with an expected one.
It turns out that, no mater what is the content of the error object, assert.deepEqual always returns true as long as the two instances being compared are Errors.
The same behaviour does not occur when comparing strings, for example (node version 4.1.1):
Reading the documentation for the deepEqual assertion, I didn't found any mentions regarding something special when comparing Errors.
Is this behaviour intended by design? Am I missing something important or should a consider another strategy to deeply compare two Error objects (instances + messages)?
The text was updated successfully, but these errors were encountered: