You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
assert.ok() throwing AssertionError instead of provided Error object #50780
If the message parameter is an instance of an Error then it will be thrown instead of the AssertionError.
I've called to it with an Error instance as second argument, and instead of being thrown that error, it's being thrown an AssertionError with it's message field set to the message field of the provided error.
Not sure what's the correct solution here, based on docs and common sense, the provided error should be thrown, but based on homogeneity, an AssertionError should be thrown (current behaviour) so it always throw an AssertionError no matter what it's provided as second argument...
How often does it reproduce? Is there a required condition?
Always.
What is the expected behavior? Why is that the expected behavior?
Not sure if it's a bug, or a documentation issue.
What do you see instead?
AssertionError is thrown, with provided Error message.
This working as expected and will throw to avoid reading a Node.js module, as doing so could potentially result in misleading errors being thrown from module.
instanceof checking may not be true depends on testing framework
My fault... I'm using Jest, and forget it has problems with instanceof and builtin classes 🤦🏻. Could make it sense to use ducktyping and check for Error objects fields? Based on spec, message, name and stackcan made us pretty sure they are Error objects themselves...
I think it wouldn't, because on child classes, the prototype constructor name would be different of Error string, so the comparison would fail and we would get to the e instanceof Error, leading to the same Jest error we have here in this issue.
I noticed that this issue is stalled even though there is an almost accepted PR that solves the problem.
I addressed the comments and fixed the problem that was blocking the CI (we'll find out on the next CI run, hehe 🐍 ).
Activity
climba03003 commentedon Nov 18, 2023
node/lib/assert.js
Lines 396 to 398 in 25fdb48
It clearly re-thrown if the message is instanceof
Error, butinstanceofchecking may not be true depends on testing framework.A minimal repo shown it is working correctly.
MrJithil commentedon Nov 21, 2023
This working as expected and will throw to avoid reading a Node.js module, as doing so could potentially result in misleading errors being thrown from module.
piranna commentedon Nov 22, 2023
My fault... I'm using Jest, and forget it has problems with
instanceofand builtin classes 🤦🏻. Could make it sense to use ducktyping and check forErrorobjects fields? Based on spec,message,nameandstackcan made us pretty sure they areErrorobjects themselves...targos commentedon Nov 22, 2023
We could use the same algorithm as
util.isError:node/lib/util.js
Lines 167 to 169 in 1858341
piranna commentedon Nov 22, 2023
It's deprecated, and also it would not work for
Errorchild classes.targos commentedon Nov 22, 2023
The function is deprecated, but not what it does. It works fine with child classes.
piranna commentedon Nov 22, 2023
I think it wouldn't, because on child classes, the prototype constructor name would be different of
Errorstring, so the comparison would fail and we would get to thee instanceof Error, leading to the same Jest error we have here in this issue.targos commentedon Nov 22, 2023
If you don't believe me, try it
BridgeAR commentedon Dec 18, 2023
We can also add a check that looks
util.types.isNativeError(). That is howassertitself checks for errors next to instanceof.piranna commentedon Dec 18, 2023
I like the idea.
NiharPhansalkar commentedon Dec 20, 2023
Hello, I would like to try and work on this issue.
piranna commentedon Dec 20, 2023
What do you need?
NiharPhansalkar commentedon Dec 20, 2023
Seeing the discussion, I will try to implement both the methods mentioned to see if it actually throws the required error. Will update accordingly?
9 remaining items
ignaciosuarezquilis commentedon May 6, 2024
Hi, can I work on this issue?
piranna commentedon May 7, 2024
Sure, go for it :-)
Naveen-2021ucp1387 commentedon May 15, 2024
is this issue still open ?
piranna commentedon May 15, 2024
Yes, it's still a problem.
pmarchini commentedon Jul 21, 2024
I noticed that this issue is stalled even though there is an almost accepted PR that solves the problem.
I addressed the comments and fixed the problem that was blocking the CI (we'll find out on the next CI run, hehe 🐍 ).
PR: #53980
P.S.: I added the author of the PR and the commenter as co-authors in the commit.
assert: use isError instead of instanceof in innerOk
piranna commentedon Jul 28, 2024
Great, good work @pmarchini :-)
assert: use isError instead of instanceof in innerOk
assert: use isError instead of instanceof in innerOk