Skip to content

Commit

Permalink
Add test cases to cover issue #47
Browse files Browse the repository at this point in the history
The problem is that expect().to.be.rejected/rejectedWith (and its negation) does not mirror how expect().to.throw (and its negation) behave.

Same issue with assert.isRejected in relation to assert.throws.
  • Loading branch information
lddubeau authored and domenic committed Sep 27, 2016
1 parent 630a32d commit deb505c
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 48 deletions.
141 changes: 113 additions & 28 deletions test/assert-promise-specific.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe "Assert interface:", =>
promise = null
error = new Error("boo")
custom = "No. I am your father."

describe "when the promise is fulfilled", =>
beforeEach =>
Expand All @@ -19,13 +20,23 @@ describe "Assert interface:", =>
op: => assert.becomes(promise, baz: "quux")
message: "to deeply equal { baz: 'quux' }"

describe ".becomes(promise, incorrectValue, custom)", =>
shouldFail
op: => assert.becomes(promise, baz: "quux", custom)
message: custom

describe ".doesNotBecome(promise, correctValue)", =>
shouldFail
op: => assert.doesNotBecome(promise, foo: "bar")
message: "to not deeply equal { foo: 'bar' }"
describe ".doesNotBecome(promise, incorrectValue)", =>
shouldPass => assert.doesNotBecome(promise, baz: "quux")

describe ".doesNotBecome(promise, correctValue, custom)", =>
shouldFail
op: => assert.doesNotBecome(promise, foo: "bar", custom)
message: custom

describe ".isRejected(promise)", =>
shouldFail
op: => assert.isRejected(promise)
Expand All @@ -38,25 +49,40 @@ describe "Assert interface:", =>
shouldFail
op: => assert.isRejected(promise, /regexp/)
message: "to be rejected"
describe ".isRejected(promise, /regexp/)", =>
describe ".isRejected(promise, TypeError, /regexp/)", =>
shouldFail
op: => assert.isRejected(promise, TypeError, /regexp/)
message: "to be rejected"
describe ".isRejected(promise, errorInstance)", =>
shouldFail
op: => assert.isRejected(promise, error)
message: "to be rejected"
# Chai never interprets the 3rd parameter to assert.throws as
# a custom error message. This is what we are checking here.
describe ".isRejected(promise, /quux/, custom)", =>
shouldFail
op: => assert.isRejected(promise, /quux/, custom)
notMessage: custom


describe "when the promise is rejected", =>
beforeEach =>
promise = rejectedPromise(error)
return undefined

describe ".isFulfilled", =>
describe ".isFulfilled(promise)", =>
shouldFail
op: => assert.isFulfilled(promise)
message: "to be fulfilled"

describe ".isFulfilled(promise, custom)", =>
shouldFail
op: => assert.isFulfilled(promise, custom)
message: custom

describe ".isRejected(promise)", =>
shouldPass => assert.isRejected(promise)

describe ".isRejected(promise, theError)", =>
shouldPass => assert.isRejected(promise, error)

Expand All @@ -65,14 +91,42 @@ describe "Assert interface:", =>
op: => assert.isRejected(promise, new Error)
message: "to be rejected with"

# Chai never interprets the 3rd parameter to assert.throws as
# a custom error message. This is what we are checking here.
describe ".isRejected(promise, differentError, custom)", =>
shouldFail
op: => assert.isRejected(promise, new Error, custom)
notMessage: custom

describe "with an Error having message 'foo bar'", =>
beforeEach =>
promise = rejectedPromise(new Error("foo bar"))
return undefined

describe ".isRejected(promise, 'bar')", =>
shouldPass => assert.isRejected(promise, "bar")

describe ".isRejected(promise, 'bar', custom)", =>
shouldPass => assert.isRejected(promise, "bar", custom)

describe ".isRejected(promise, /bar/)", =>
shouldPass => assert.isRejected(promise, /bar/)

describe ".isRejected(promise, /bar/, custom)", =>
shouldPass => assert.isRejected(promise, /bar/, custom)

describe ".isRejected(promise, 'quux')", =>
shouldFail
op: => assert.isRejected(promise, "quux")
message: "to be rejected with"

# Chai 3.5.0 never interprets the 3rd parameter to assert.throws as
# a custom error message. This is what we are checking here.
describe ".isRejected(promise, 'quux', custom)", =>
shouldFail
op: => assert.isRejected(promise, "quux", custom)
notMessage: custom

describe ".isRejected(promise, /quux/)", =>
shouldFail
op: => assert.isRejected(promise, /quux/)
Expand All @@ -85,40 +139,71 @@ describe "Assert interface:", =>

describe ".isRejected(promise, RangeError)", =>
shouldPass => assert.isRejected(promise, RangeError)

describe ".isRejected(promise, TypeError)", =>
shouldFail
op: => assert.isRejected(promise, TypeError)
message: "to be rejected"

describe "Assertion messages", =>
message = "No. I am your father."
describe "with a RangeError having a message 'foo bar'", =>
beforeEach =>
promise = rejectedPromise(new RangeError("foo bar"))
return undefined

describe "should be passed through for .isFulfilled(promise, message)", =>
shouldFail
op: => assert.isFulfilled(rejectedPromise(), message)
message: message
describe ".isRejected(promise, RangeError, 'foo')", =>
shouldPass => assert.isRejected(promise, RangeError, "foo")

describe "should be passed through for .isRejected(promise, message)", =>
shouldFail
op: => assert.isRejected(fulfilledPromise(), message)
message: message
describe ".isRejected(promise, RangeError, /bar/)", =>
shouldPass => assert.isRejected(promise, RangeError, /bar/)

describe "should be passed through for .isRejected(promise, TypeError, message)", =>
shouldFail
op: => assert.isRejected(fulfilledPromise(), TypeError, message)
message: message
describe ".isRejected(promise, RangeError, 'quux')", =>
shouldFail
op: => assert.isRejected(promise, RangeError, "quux")
message: "to be rejected with an error including 'quux' but got 'RangeError: foo bar'"

describe "should be passed through for .isRejected(promise, /regexp/, message)", =>
shouldFail
op: => assert.isRejected(fulfilledPromise(), /regexp/, message)
message: message
describe ".isRejected(promise, RangeError, /quux/)", =>
shouldFail
op: => assert.isRejected(promise, RangeError, /quux/)
message: "to be rejected with an error matching /quux/ but got 'RangeError: foo bar'"

describe "should be passed through for .becomes(promise, incorrectValue, message)", =>
shouldFail
op: => assert.becomes(fulfilledPromise(baz: "quux"), foo: "bar", message)
message: message
describe ".isRejected(promise, TypeError, 'foo')", =>
shouldFail
op: => assert.isRejected(promise, TypeError, 'foo')
message: "to be rejected with 'TypeError' but it was rejected with [RangeError: foo bar]"
describe ".isRejected(promise, TypeError, /bar/)", =>
shouldFail
op: => assert.isRejected(promise, TypeError, /bar/)
message: "to be rejected with 'TypeError' but it was rejected with [RangeError: foo bar]"

describe "should be passed through for .doesNotBecome(promise, incorrectValue, message)", =>
shouldFail
op: => assert.doesNotBecome(fulfilledPromise(foo: "bar"), foo: "bar", message)
message: message
describe ".isRejected(promise, TypeError, 'quux')", =>
shouldFail
op: => assert.isRejected(promise, TypeError, 'quux')
message: "to be rejected with 'TypeError' but it was rejected with [RangeError: foo bar]"
describe ".isRejected(promise, TypeError, /quux/)", =>
shouldFail
op: => assert.isRejected(promise, TypeError, /quux/)
message: "to be rejected with 'TypeError' but it was rejected with [RangeError: foo bar]"

describe ".isRejected(promise, RangeError, 'foo', custom)", =>
shouldPass => assert.isRejected(promise, RangeError, "foo", custom)

describe ".isRejected(promise, RangeError, /bar/, custom)", =>
shouldPass => assert.isRejected(promise, RangeError, /bar/, custom)

describe ".isRejected(promise, RangeError, 'quux', custom)", =>
shouldFail
op: => assert.isRejected(promise, RangeError, "quux", custom)
message: custom

describe ".isRejected(promise, RangeError, /quux/, custom)", =>
shouldFail
op: => assert.isRejected(promise, RangeError, /quux/, custom)
message: custom

describe ".isRejected(promise, RangeError, undefined, custom)", =>
shouldPass => assert.isRejected(promise, RangeError, undefined, custom)

describe ".isRejected(promise, TypeError, undefined, custom)", =>
shouldFail
op: => assert.isRejected(promise, TypeError, undefined, custom)
message: custom
Loading

0 comments on commit deb505c

Please sign in to comment.