Skip to content

Commit

Permalink
Adding .rejected.with(errorInstance).
Browse files Browse the repository at this point in the history
This matches the 0.6.x release of Chai: see logicalparadox/chai@41f645dbb8590346e24eb2feddd010962c39fa26
  • Loading branch information
domenic committed Apr 9, 2012
1 parent 5f9acf7 commit 4ce32cf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
30 changes: 30 additions & 0 deletions lib/chai-as-promised.js
Expand Up @@ -106,9 +106,15 @@

// Augment the transformed promise with a `with` method that performs further transformations.
transformedPromise.with = function (Constructor, message) {
var desiredReason = null;

if (Constructor instanceof RegExp || typeof Constructor === "string") {
message = Constructor;
Constructor = null;
} else if (Constructor && Constructor instanceof Error) {
desiredReason = Constructor;
Constructor = null;
message = null;
}

var messageVerb = null;
Expand All @@ -130,8 +136,19 @@
return rejectionReason instanceof Constructor;
}

function matchesDesiredReason() {
return rejectionReason === desiredReason;
}

var onTransformedFulfilled = function () {
if (!this.negate) {
if (desiredReason) {
this.assert(matchesDesiredReason(),
null,
"expected " + this.inspect + " to be rejected with " + chai.inspect(desiredReason) +
"but it was rejected with " + chai.inspect(rejectionReason));
}

if (Constructor) {
this.assert(constructorIsGood(),
"expected " + this.inspect + " to be rejected with " + Constructor.prototype.name +
Expand All @@ -148,6 +165,13 @@

var onTransformedRejected = function () {
if (this.negate) {
if (desiredReason) {
this.assert(matchesDesiredReason(),
null,
"expected " + this.inspect + " to not be rejected with " +
chai.inspect(desiredReason));
}

if (Constructor) {
this.assert(constructorIsGood(),
null,
Expand All @@ -162,6 +186,12 @@
" " + message);
}
} else {
if (desiredReason) {
this.assert(false,
"expected " + this.inspect + " to be rejected with " + chai.inspect(desiredReason) +
"but it was fulfilled");
}

if (Constructor) {
this.assert(false, "expected " + this.inspect + " to be rejected with " +
Constructor.prototype.name + " but it was fulfilled");
Expand Down
19 changes: 17 additions & 2 deletions test/promiseSpecific.coffee
@@ -1,5 +1,6 @@
describe "Promise-specific extensions:", ->
promise = null
error = new Error("boo")

describe "when the promise is fulfilled", ->
beforeEach ->
Expand All @@ -22,6 +23,8 @@ describe "Promise-specific extensions:", ->
shouldFail -> promise.should.be.rejected.with(TypeError, "message substring")
describe ".rejected.with(TypeError, /regexp/)", ->
shouldFail -> promise.should.be.rejected.with(TypeError, /regexp/)
describe ".rejected.with(errorInstance)", ->
shouldFail -> promise.should.be.rejected.with(error)

describe ".not.rejected", ->
shouldPass -> promise.should.not.be.rejected
Expand All @@ -35,17 +38,29 @@ describe "Promise-specific extensions:", ->
shouldPass -> promise.should.not.be.rejected.with(TypeError, "message substring")
describe ".not.rejected.with(TypeError, /regexp/)", ->
shouldPass -> promise.should.not.be.rejected.with(TypeError, /regexp/)
describe ".not.rejected.with(errorInstance)", ->
shouldPass -> promise.should.not.be.rejected.with(error)

describe "when the promise is rejected", ->
beforeEach ->
promise = Q.reject(new Error)
promise = Q.reject(error)

describe ".fulfilled", ->
shouldFail -> promise.should.be.fulfilled
describe ".not.fulfilled", ->
shouldPass -> promise.should.not.be.fulfilled

describe "with an error having message 'foo bar'", ->
describe ".rejected.with(theError)", ->
shouldPass -> promise.should.be.rejected.with(error)
describe ".not.rejected.with(theError)", ->
shouldFail -> promise.should.not.be.rejected.with(error)

describe ".rejected.with(differentError)", ->
shouldFail -> promise.should.be.rejected.with(new Error)
describe ".not.rejected.with(differentError)", ->
shouldPass -> promise.should.not.be.rejected.with(new Error)

describe "with an Error having message 'foo bar'", ->
beforeEach ->
promise = Q.reject(new Error("foo bar"))

Expand Down

0 comments on commit 4ce32cf

Please sign in to comment.