镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@piranna

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

Activity

  1. climba03003 commented on Nov 18, 2023

    @climba03003
    Contributor

    node/lib/assert.js

    Lines 396 to 398 in 25fdb48

    } else if (message instanceof Error) {
    throw message;
    }

    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'))
  2. added
    assertIssues and PRs related to the assert subsystem.
    on Nov 19, 2023
  3. MrJithil commented on Nov 21, 2023

    @MrJithil
    Member

    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.

  4. piranna commented on Nov 22, 2023

    @piranna
    ContributorAuthor

    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...

  5. targos commented on Nov 22, 2023

    @targos
    Member

    We could use the same algorithm as util.isError:

    node/lib/util.js

    Lines 167 to 169 in 1858341

    function isError(e) {
    return ObjectPrototypeToString(e) === '[object Error]' || e instanceof Error;
    }

  6. piranna commented on Nov 22, 2023

    @piranna
    ContributorAuthor

    We could use the same algorithm as util.isError

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

  7. targos commented on Nov 22, 2023

    @targos
    Member

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

  8. piranna commented on Nov 22, 2023

    @piranna
    ContributorAuthor

    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.

  9. targos commented on Nov 22, 2023

    @targos
    Member

    If you don't believe me, try it

  10. BridgeAR commented on Dec 18, 2023

    @BridgeAR
    Member

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

  11. piranna commented on Dec 18, 2023

    @piranna
    ContributorAuthor

    I like the idea.

  12. NiharPhansalkar commented on Dec 20, 2023

    @NiharPhansalkar
    Contributor

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

  13. piranna commented on Dec 20, 2023

    @piranna
    ContributorAuthor

    What do you need?

  14. NiharPhansalkar commented on Dec 20, 2023

    @NiharPhansalkar
    Contributor

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

  15. 9 remaining items

  16. ignaciosuarezquilis commented on May 6, 2024

    @ignaciosuarezquilis

    Hi, can I work on this issue?

  17. piranna commented on May 7, 2024

    @piranna
    ContributorAuthor

    Hi, can I work on this issue?

    Sure, go for it :-)

  18. Naveen-2021ucp1387 commented on May 15, 2024

    @Naveen-2021ucp1387

    is this issue still open ?

  19. piranna commented on May 15, 2024

    @piranna
    ContributorAuthor

    Yes, it's still a problem.

  20. pmarchini commented on Jul 21, 2024

    @pmarchini
    Member

    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.

  21. piranna commented on Jul 28, 2024

    @piranna
    ContributorAuthor

    Great, good work @pmarchini :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    assertIssues and PRs related to the assert subsystem.good first issueIssues that are suitable for first-time contributors.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions