-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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 default operator to assert.fail()
#22694
Changes from 1 commit
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 |
---|---|---|
|
@@ -87,8 +87,9 @@ function innerFail(obj) { | |
function fail(actual, expected, message, operator, stackStartFn) { | ||
const argsLen = arguments.length; | ||
|
||
let internalMessage; | ||
if (argsLen === 0) { | ||
message = 'Failed'; | ||
internalMessage = 'Failed'; | ||
} else if (argsLen === 1) { | ||
message = actual; | ||
actual = undefined; | ||
|
@@ -106,13 +107,23 @@ function fail(actual, expected, message, operator, stackStartFn) { | |
operator = '!='; | ||
} | ||
|
||
innerFail({ | ||
if (message instanceof Error) throw message; | ||
|
||
const errArgs = { | ||
actual, | ||
expected, | ||
message, | ||
operator, | ||
operator: operator || 'fail', | ||
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. Does Node have a consistent way of checking for optional params or default params? 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. No, it's often mixed and fasly values are often set to a default while in other cases only undefined is set to a default. 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. This method has a mix of deprecated/legacy and new/different functionality. Would moving the operator juggle into a 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. That was done on purpose since I believe it is best to just properly do this for all cases. Shall I maybe change the check to: |
||
stackStartFn: stackStartFn || fail | ||
}); | ||
}; | ||
if (message !== undefined) { | ||
errArgs.message = message; | ||
} | ||
const err = new AssertionError(errArgs); | ||
if (internalMessage) { | ||
err.message = internalMessage; | ||
err.generatedMessage = true; | ||
} | ||
throw err; | ||
} | ||
|
||
assert.fail = fail; | ||
|
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.
☝️ Should use the util for
isError
(internal reference) for this since it avoids the error of errors from other realms.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.
Only changing this here is likely not the best way. It would have to be changed throughout assert in total or not at all. It is also sad that the util function is not a safe check as the
Symbol.toStringTag
could be set andisError
would happily return true in some weird cases.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.
Ah, didn't know this was consistent with the rest of
assert
. A follow up would be best.Oh I thought the new
isNativeError
was more robust.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.
Oh, I missed the
isNativeError
somehow. We have multiple places in core where we should likely switch to this check.