Conversation
|
Where do we use this behavior? |
|
@tniessen every error thrown by assert have a couple more properties and currently they are tested only for assert.fail. In general I think it makes sense to test for everything that is requested for and not only for some of these properties. |
test/common/index.js
Outdated
There was a problem hiding this comment.
I'm thinking this needs a custom message The ${key} field of ${error} was ${error[key]} and not {options[key]} as expected (or a little bit shorter)
Otherwise we get an AssertionError: a !== b thrown from deep within the harness.
So the same applies for L716 and the message strings.
test/common/index.js
Outdated
There was a problem hiding this comment.
Would a switch look better?
New code looks real pretty 💅
test/common/index.js
Outdated
There was a problem hiding this comment.
If we keep the if/else change to (key === 'message' && options.message instanceof RegExp) and let the string case fall to the else
benjamingr
left a comment
There was a problem hiding this comment.
I'm (not strongly) -1 on this change. I don't think every single property should match the options.
It complicates the code a little and I'm not sure this is a good idea.
|
Well I guess the point is to make it flexible. Instead of forcing at max |
|
I understand the point and appreciate the effort - but I think it makes the code more confusing for no good reason at the moment - and I can see the pitfalls of tests failing after an object is passed and a property is added unintentionally. If it was called |
It will fail also as is with unintentionally added properties ( |
|
@benjamingr I ran into this with another PR as I wanted to test additional properties than just You have to explicitly list every property that you want to test for and if you do not want to test any besides e.g. const err = new Error('foobar')
err.code = 'RANDOM'
err.additional = true
err.foobar = 'is ignored'
// Tests only code
common.expectsError({
code: 'RANDOM'
})(err)
// Tests other properties
common.expectsError({
code: 'RANDOM',
additional: true
type: Error
})(err) |
As I see it the crux of the issue is that the new The main problem with the current code is the destructuring of the formal agrument: P.S. @benjamingr IMHO the new version of the code is almost as succinct as the code before... and fixes the problem of unuseful messages.. so⚖️ Ref: #13686 |
|
Also relevant #13937 if message change are |
test/common/index.js
Outdated
There was a problem hiding this comment.
If it was called
.expectsand not.expectsErroror just deepEqual I would understand.
That made me think, remove the if and that will make options.type mandatory.
Also add assert(expected instanceof Error)if (!(expected instanceof Error)) throw new TypeError('`options.type` must inherit from `Error`');
There was a problem hiding this comment.
That does not work as the class is passed to the function and not the instance. The simplest check I could though of is: expected !== Error && !Error.isPrototypeOf(expected)
|
@BridgeAR I don't want to mix 🍎s and 🍊s, but we should actually add a validation that |
01d4c28 to
a635532
Compare
|
Rebased due to conflicts and I updated the documentation as I missed that. @benjamingr would you mind having another look? |
|
I'm fine with asserting that it's an instance of an error - but I'm not sure about testing all properties. I'm concerned about cases where a "dirty" error object would be passed and it would verify incorrect things by mistake. I'm not sure I see the value this adds to be honest. The method is already a little footgunny for verifying things by convention - going over all object properties and treating some of them differently doesn't sound like good design to me. |
|
I'm not sure I understand what you mean with the "dirty" error object. Do you mean the As you can see there is no change needed for any test and the function will only test properties explicitly asked for in the For me the current situation was a minor footgun by not testing properties that I wanted it to test for. |
I mean someone passing a |
Ack. Probably the best thing would be to add |
test/common/README.md
Outdated
There was a problem hiding this comment.
This additional bit subclassed by the 'internal/errors' module doesn't really make sense. The thing must be an instance of type and must be an Error subclass. Whether it is a subclass of one of the internal/errors.js classes is somewhat inconsequential.
test/common/README.md
Outdated
There was a problem hiding this comment.
Need a blank line after the * return line... and it should be * Returns:
There was a problem hiding this comment.
The * Returns: would be inconsistent with all other entries in this file. Shall I change all other entries as well?
a635532 to
82d6c48
Compare
benjamingr
left a comment
There was a problem hiding this comment.
New version not using Object.keys looks good to me
|
I completely reworked the code as suggested by @refack |
I had this whole spiel about flattening
|
f411221 to
00479f6
Compare
|
Rebased. @refack could this land or do you want me to still change something? |
|
Didn't see that the CR was dismissed. |
PR-URL: nodejs#14058 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
|
Landed in 2a621d4 |
|
Extra sanity run on |
|
Should this be backported to |
|
This function does not exist in v.6 so backporting is obsolete. |
So far common.expectsError only tested
type,messageandcodeinstead of all properties.Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)
test