node icon indicating copy to clipboard operation
node copied to clipboard

`assert.ok()` throwing `AssertionError` instead of provided `Error` object

Open piranna opened this issue 2 years ago • 22 comments

Version

v20.9.0

Platform

Linux executive 6.5.0-10-generic #10-Ubuntu SMP PREEMPT_DYNAMIC Fri Oct 13 13:49:38 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

assert

What steps will reproduce the bug?

assert.ok() docs says:

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.

Additional information

No response

piranna avatar Nov 18 '23 12:11 piranna

https://github.com/nodejs/node/blob/25fdb48876e1c1f2c7e16506b2e1c5bf4b404ecf/lib/assert.js#L396-L398

It clearly re-thrown if the message is instanceof Error, but instanceof checking may not be true depends on testing framework.

A minimal repo shown it is working correctly.

import { ok } from 'assert/strict'

ok(false, Error('here is error'))

climba03003 avatar Nov 18 '23 13:11 climba03003

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.

MrJithil avatar Nov 21 '23 02:11 MrJithil

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 stack can made us pretty sure they are Error objects themselves...

piranna avatar Nov 22 '23 04:11 piranna

We could use the same algorithm as util.isError:

https://github.com/nodejs/node/blob/1858341377c268c5ffce9345517dc07b0e6c240a/lib/util.js#L167-L169

targos avatar Nov 22 '23 12:11 targos

We could use the same algorithm as util.isError

It's deprecated, and also it would not work for Error child classes.

piranna avatar Nov 22 '23 12:11 piranna

The function is deprecated, but not what it does. It works fine with child classes.

targos avatar Nov 22 '23 12:11 targos

It works fine with child classes.

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.

piranna avatar Nov 22 '23 12:11 piranna

If you don't believe me, try it

targos avatar Nov 22 '23 12:11 targos

We can also add a check that looks util.types.isNativeError(). That is how assert itself checks for errors next to instanceof.

BridgeAR avatar Dec 18 '23 14:12 BridgeAR

I like the idea.

piranna avatar Dec 18 '23 16:12 piranna

Hello, I would like to try and work on this issue.

NiharPhansalkar avatar Dec 20 '23 04:12 NiharPhansalkar

What do you need?

piranna avatar Dec 20 '23 06:12 piranna

Seeing the discussion, I will try to implement both the methods mentioned to see if it actually throws the required error. Will update accordingly?

NiharPhansalkar avatar Dec 20 '23 07:12 NiharPhansalkar

The wrong Error class is being dispatched due to using Jest, it's a bug there due to how it manage native classes. Proper solution should be done there, but in addition to that, a isNativeError() function can be useful too, at least to bypass the problem here and in other places.

piranna avatar Dec 20 '23 07:12 piranna

So you initially want to check if a function similar to isNativeError() returns true, and if it does, just directly throw that error instead of bothering with how instanceof is treated?

NiharPhansalkar avatar Dec 20 '23 07:12 NiharPhansalkar

I think so, yes.

piranna avatar Dec 20 '23 08:12 piranna

@piranna Is there any way to get Jest to use my local nodejs? i.e the node I have in node/out/Release/node?

I tried searching around but can't find much. Maybe you have tried this before?

NiharPhansalkar avatar Dec 20 '23 14:12 NiharPhansalkar

Compile your Node.js, and launch Jest with It.

piranna avatar Dec 20 '23 14:12 piranna

I have compiled my node, how do I launch Jest with it? Is there some particular config option for it? Or a command line flag?

NiharPhansalkar avatar Dec 20 '23 14:12 NiharPhansalkar

out/Release/node path/to/jest/cli.js

targos avatar Dec 20 '23 14:12 targos

Hi, can I work on this issue?

ignaciosuarezquilis avatar May 06 '24 01:05 ignaciosuarezquilis

Hi, can I work on this issue?

Sure, go for it :-)

piranna avatar May 07 '24 09:05 piranna

is this issue still open ?

Naveen-2021ucp1387 avatar May 15 '24 20:05 Naveen-2021ucp1387

Yes, it's still a problem.

piranna avatar May 15 '24 20:05 piranna

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.

pmarchini avatar Jul 21 '24 15:07 pmarchini

Great, good work @pmarchini :-)

piranna avatar Jul 28 '24 13:07 piranna