Skip to content
This repository has been archived by the owner on Nov 4, 2020. It is now read-only.

Commit

Permalink
Added .resolved, .resolvedWith as aliases to .fulfilled, .fulfilledWith
Browse files Browse the repository at this point in the history
  • Loading branch information
btd committed Sep 13, 2017
1 parent e90c2cf commit fe6b136
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 40 deletions.
25 changes: 10 additions & 15 deletions lib/ext/promise.js
Expand Up @@ -36,6 +36,7 @@ export default function(should, Assertion) {
*
* @name fulfilled
* @memberOf Assertion
* @alias Assertion#resolved
* @returns {Promise}
* @category assertion promises
* @example
Expand Down Expand Up @@ -64,15 +65,16 @@ export default function(should, Assertion) {
},
function next$onReject(err) {
if (!that.negate) {
that.params.operator +=
", but it was rejected with " + should.format(err);
that.params.operator += ", but it was rejected with " + should.format(err);
that.fail();
}
return err;
}
);
};

Assertion.prototype.resolved = Assertion.prototype.fulfilled;

/**
* Assert given promise will be rejected. Result of assertion is still .thenable and should be handled accordingly.
*
Expand Down Expand Up @@ -124,6 +126,7 @@ export default function(should, Assertion) {
*
* @name fulfilledWith
* @memberOf Assertion
* @alias Assertion#resolvedWith
* @category assertion promises
* @returns {Promise}
* @example
Expand Down Expand Up @@ -156,15 +159,16 @@ export default function(should, Assertion) {
},
function next$onError(err) {
if (!that.negate) {
that.params.operator +=
", but it was rejected with " + should.format(err);
that.params.operator += ", but it was rejected with " + should.format(err);
that.fail();
}
return err;
}
);
};

Assertion.prototype.resolvedWith = Assertion.prototype.fulfilledWith;

/**
* Assert given promise will be rejected with some sort of error. Arguments is the same for Assertion#throw.
* Result of assertion is still .thenable and should be handled accordingly.
Expand Down Expand Up @@ -233,18 +237,9 @@ export default function(should, Assertion) {

if (!errorMatched) {
if (typeof message === "string" || message instanceof RegExp) {
errorInfo =
" with a message matching " +
should.format(message) +
", but got '" +
err.message +
"'";
errorInfo = " with a message matching " + should.format(message) + ", but got '" + err.message + "'";
} else if ("function" === typeof message) {
errorInfo =
" of type " +
functionName(message) +
", but got " +
functionName(err.constructor);
errorInfo = " of type " + functionName(message) + ", but got " + functionName(err.constructor);
}
} else if ("function" === typeof message && properties) {
try {
Expand Down
4 changes: 3 additions & 1 deletion should.d.ts
@@ -1,6 +1,6 @@
// Type definitions for should.js

declare function should(obj: any): should.Assertion
declare function should(obj: any): should.Assertion;

// node assert methods
/*interface NodeAssert {
Expand Down Expand Up @@ -164,9 +164,11 @@ declare namespace should {
Promise(): this;

fulfilled: Promise<any>;
resolved: Promise<any>;
rejected: Promise<any>;

fulfilledWith(obj: any): Promise<any>;
resolvedWith(obj: any): Promise<any>;
rejectedWith(msg: RegExp | string | Error, properties?: {});
rejectedWith(properties: {});
finally: PromisedAssertion;
Expand Down
35 changes: 11 additions & 24 deletions test/ext/promise.test.js
Expand Up @@ -42,10 +42,7 @@ it("should allow to use .not and .any", function() {
});

it("should treat assertion like promise", function() {
return Promise.all([
promised(10).should.finally.be.a.Number(),
promised("abc").should.finally.be.a.String()
]);
return Promise.all([promised(10).should.finally.be.a.Number(), promised("abc").should.finally.be.a.String()]);
});

it("should propagate .not before .finally", function() {
Expand Down Expand Up @@ -80,12 +77,12 @@ it("should allow to check if promise fulfilled", function() {
should.fail();
},
function(err) {
err.should.be
.Error()
.and.match({ message: "expected [Promise] not to be fulfilled" });
err.should.be.Error().and.match({ message: "expected [Promise] not to be fulfilled" });
}
),
promiseFail().should.not.be.fulfilled() //negative fail
promiseFail().should.not.be.fulfilled(), //negative fail
promised(10).should.be.resolved(),
promised(10).should.be.resolvedWith(10)
]);
});

Expand Down Expand Up @@ -134,8 +131,7 @@ it("should be allow to check if promise rejected", function() {
},
function(err) {
err.should.be.Error().and.match({
message:
"expected [Promise] to be rejected, but it was fulfilled with 10"
message: "expected [Promise] to be rejected, but it was fulfilled with 10"
});
}
),
Expand All @@ -147,9 +143,7 @@ it("should be allow to check if promise rejected", function() {
should.fail();
},
function(err) {
err.should.be
.Error()
.and.match({ message: "expected [Promise] not to be rejected" });
err.should.be.Error().and.match({ message: "expected [Promise] not to be rejected" });
}
),
promised(10).should.not.be.rejected() //negative fail
Expand All @@ -168,8 +162,7 @@ it("should allow to match rejected error", function() {
},
function(err) {
return err.should.be.Error().and.match({
message:
"expected [Promise] to be rejected with a message matching 'boom1', but got 'boom'"
message: "expected [Promise] to be rejected with a message matching 'boom1', but got 'boom'"
});
}
),
Expand All @@ -184,9 +177,7 @@ it("should allow to match rejected error", function() {
return should.fail();
},
function(err) {
return err.should.be
.Error()
.and.match({ message: "expected [Promise] not to be rejected" });
return err.should.be.Error().and.match({ message: "expected [Promise] not to be rejected" });
}
),
promised(10)
Expand All @@ -197,9 +188,7 @@ it("should allow to match rejected error", function() {
return should.fail();
},
function(err) {
return err.should.be
.Error()
.and.match({ message: "expected [Promise] to be rejected" });
return err.should.be.Error().and.match({ message: "expected [Promise] to be rejected" });
}
),
promiseFail()
Expand All @@ -210,9 +199,7 @@ it("should allow to match rejected error", function() {
return should.fail();
},
function(err) {
return err.should.be
.Error()
.and.match({ message: "expected [Promise] not to be rejected" });
return err.should.be.Error().and.match({ message: "expected [Promise] not to be rejected" });
}
)
]);
Expand Down

0 comments on commit fe6b136

Please sign in to comment.