Skip to content
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

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

Closed
bnoordhuis opened this issue Jan 9, 2015 · 9 comments

Comments

@bnoordhuis
Copy link
Member

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.

@cjihrig
Copy link
Contributor

cjihrig commented Jan 9, 2015

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.

@dougwilson
Copy link
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).

@dougwilson
Copy link
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).

@cjihrig
Copy link
Contributor

cjihrig commented Jan 9, 2015

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.

@bnoordhuis
Copy link
Member Author

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.

@cjihrig
Copy link
Contributor

cjihrig commented Jan 9, 2015

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

@caitp
Copy link
Contributor

caitp commented Jan 9, 2015

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)

cjihrig added a commit that referenced this issue Jan 12, 2015
Currently, anything passed as the block argument to throws()
and doesNotThrow() is interpreted as a function, which can
lead to unexpected results. This commit checks the type of
block, and throws a TypeError if it is not a function.

Fixes: #275
PR-URL: #308
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
@cjihrig
Copy link
Contributor

cjihrig commented Jan 12, 2015

Closed in 14dc917

@cjihrig cjihrig closed this as completed Jan 12, 2015
@caitp
Copy link
Contributor

caitp commented Jan 12, 2015

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants