Skip to content

lib: assert.throws('string', TypeError, 'whatever') passes when it shouldn't #275

Description

@bnoordhuis
assert.throws('string', TypeError, 'whatever');  // should fail but doesn't

From here:

assert.throws() calls the first argument as a function. The result is a TypeError when the input is a string but it doesn't seem to check that the exception message matches.

Activity

  1. cjihrig commented on Jan 9, 2015

    @cjihrig
    Contributor

    The third argument isn't supposed to be validated is it? The documentation doesn't seem to imply that it should. To validate the exception message, the second argument should be a regex or function.

    The third argument should be included in the error message though, right? That doesn't seem to be happening.

  2. dougwilson commented on Jan 9, 2015

    @dougwilson
    Member

    The third argument is the message to put in the AssertionError, like like all the other stuff in assert (the last argument is the assertion message).

  3. dougwilson commented on Jan 9, 2015

    @dougwilson
    Member

    Checking the thrown exception message would be to give a string as the second argument, rather than a constructor (you can also provide a RegExp object as the second argument).

  4. cjihrig commented on Jan 9, 2015

    @cjihrig
    Contributor

    Expects block to throw an error. error can be constructor, RegExp or validation function.

    I don't think strings are supported as the second arguments.

    Is there a specific reason that the assert module doesn't do a better job at input validation? For example, instead of blindly treating the first argument to throws() as a function, maybe it should validate that it is actually a function.

  5. bnoordhuis commented on Jan 9, 2015

    @bnoordhuis
    MemberAuthor

    Alright, seems I was wrong about the third argument. Let me rephrase: assert.throws('string', TypeError) passes for the wrong reasons. I think it should either throw a TypeError ("first argument must be a function") or eval() the argument, like the assert functions in the SM and V8 test suites.

  6. cjihrig commented on Jan 9, 2015

    @cjihrig
    Contributor

    @bnoordhuis +1 on throwing on bad input. I'll work on this.

  7. caitp commented on Jan 9, 2015

    @caitp
    Contributor

    what if the assert library were just changed to eval() if the first argument is a string? mjsunit does it like this, it's convenient :> (as suggested above, I guess)

  8. cjihrig commented on Jan 12, 2015

    @cjihrig
    Contributor

    Closed in 14dc917

  9. caitp commented on Jan 12, 2015

    @caitp
    Contributor

    Think I like the v8 behavior better there

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions