Description
This is my proposal for resolving #661. In short I want to stop deferring to https://nodejs.org/api/assert.html#assert_assert_throws_block_error_message and remove various parameter overloads that are currently supported by the assertion.
Here's what I'd like to support:
t.throws(fn) // Throws an Error (or subclass thereof)
t.throws(fn, SyntaxError) // Throws a SyntaxError (or subclass thereof)
t.throws(fn, 'string') // Throws an Error (or subclass thereof), whose .message === 'string'
t.throws(fn, /regexp/) // Throws an Error (or subclass thereof), whose /regexp/.test(err.message) === true
If you need to test the errors class and message you should use the t.throws
return value:
const err = t.throws(fn, SyntaxError)
t.true(err.message === 'expecting integer')
Passing anything other than undefined
, functions, strings or regular expressions as the second argument results in an TypeError
from t.throws()
itself.
The first argument is allowed to be a promise or observable, as is any return value from the fn
call. Of course this makes t.throws
asynchronous, so users may need to do:
const err = await t.throws(fn)
Questions:
-
Does
t.throws(fn, Constructor)
require the thrown value to be anError
? For example:t.throws(() => { throw 'foo' }, String) // Is this allowed?
-
Should
t.throws(fn, 'string')
andt.throws(fn, /regexp/)
be supported at all, or would it be preferable to dereference the error and then use another assertion?const err = t.throws(fn) t.true(err.message === 'expecting integer')
-
Is there a need for
t.throws(fn, SyntaxError, 'string')
andt.throws(fn, SyntaxError, /regexp/)
-
Does anybody have experience with / examples of asserting
Error
instances across contexts?