-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Reimplement t.throws() and t.notThrows() #1704
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
Conversation
Move tests from test/promise.js and test/observable.js.
a4cd3ea
to
d467e72
Compare
The expectation object works now. The various examples run to hundreds of lines, so please expand the details below:
|
I don't think From the original issue:
Could you do this? |
What do you think about dropping support for |
I'm not sure it's necessary. We always check that the value is an error, so if it's not then you'd never get to the
I like the brevity of
That depends how tightly you want to couple your test to any current error message. E.g. when validating input type checks, looking for
|
readme.md
Outdated
|
||
Returns the error thrown by `function` or a promise for the rejection reason of the specified `promise`. | ||
`expected` can be a constructor, in which case the thrown error must be an instance. It can be a string, which is compared against the thrown error's message, or a regular expression which is matched against this message. You can also specify a matcher object with one or more of the following properties: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
must be an instance
=> must be an instance of the constructor
readme.md
Outdated
* `name`: the expected `.name` value of the thrown error | ||
* `of`: a constructor, the thrown error must be an instance of | ||
|
||
`expected` does not need to be specified. If you don't need it but do want to set an assertion message you have to specify `null` or the empty object `{}`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're being strict, can we only support null
? Doesn't make sense to specify an empty object.
I like the brevity too, but I feel we're gonna get the same complaints as we got with
But then you don't know whether the TypeError is coming directly from your function or some other function you're calling. I'm fine with leaving it, just thought I would mention how it can cause subtle issues.
That's when I would use it, yeah, but it's very rare that I create custom errors. |
Good point. I didn't think about that when commenting back then. |
I've been playing with this PR and seems to work perfectly. |
@sindresorhus addressed feedback. |
Remove `core-assert` dependency. When passed a function as the second argument, `t.throws()` now assumes its a constructor. This removes support for a validation function, which may or may not have worked. Refs #1047. `t.throws()` now fails if the exception is not an error. Fixes #1440. Regular expressions are now matched against the error message, not the result of casting the error to a string. Fixes #1445. Validate second argument to `t.throws()`. Refs #1676. Assertion failures now display how AVA arrived at the exception. Constructors are printed when the error is not a correct instance. Fixes #1471.
Fixes #1047. Fixes #1676. A combination of the following expectations is supported: ```js t.throws(fn, {of: SyntaxError}) // err instanceof SyntaxError t.throws(fn, {name: 'SyntaxError'}) // err.name === 'SyntaxError' t.throws(fn, {is: expectedErrorInstance}) // err === expectedErrorInstance t.throws(fn, {message: 'expected error message'}) // err.message === 'expected error message' t.throws(fn, {message: /expected error message/}) // /expected error message/.test(err.message) ```
6d7c019
to
94ac094
Compare
This is causing problems migrating an older module for me. [email protected] rejects with a |
@shellscape we don't have consensus in the Core team to change this behavior. Would it be possible though to inherit |
Implement
t.throws()
andt.notThrows()
ourselves.Remove
core-assert
dependency. When passed a function as the second argument,t.throws()
now assumes it's a constructor. This removes support for a validation function, which may or may not have worked. Refs #1047.t.throws()
now fails if the exception is not an error. Fixes #1440.Regular expressions are now matched against the error message, not the result of casting the error to a string. Fixes #1445.
Validate second argument to
t.throws()
. Refs #1676.Assertion failures now display how AVA arrived at the exception. Constructors are printed when the error is not a correct instance. Fixes #1471.
Still need to support an expectation object. I'm planning to implement this alongside the current support for passing constructors, strings or regular expressions. The shorthand form seems useful and this way we won't have too many backward compatibility issues.
I'll be looking to support (a combination of) the following use cases:
I'll try to post some screenshots too.