What nicholaswmin said is correct, but there's a "rest of the owl" moment here that I'd like to clear-up.
If your desire is: "I want to assert reject", i.e. "I want to make sure this code rejects in the right way," the pattern is a bit complicated.
First off, write in return fxnWhichReturnsPromise()
. The test will fail if your function is rejecting, and you want the inverse of that. You have to use the two-argument signature of .then()
, which looks like: .then(onFulfillCallback, onRejectCallback)
, since if you split it into .then
& .catch
, failing in the .then
-block will be caught by the subsequent .catch
-block, and what should be a failing test (it did not reject) will be a passing one.
In practice, altogether it looks something like this:
it('rejects when given foobar', () => {
return fxnThatShouldReject(foobar)
.then(
(val) => { assert.fail(`Should have rejected, returned with ${val} instead`)
,
(err) => { assert.equal(err.message, "Rejection reason I was expecting") }
)
})
The reason the assertion library does not have a built-in assertion handler for this is because being able to "hold-up" asynchronous execution in the test suite is really a test-runner capability. assert.throws()
is synchronous, by contrast.