Skip to content

Commit

Permalink
fix: raise a nicer error when non-object errors are matched (#294)
Browse files Browse the repository at this point in the history
When a promise is rejected, we currently incorrectly assume the
rejection reason is an object consumable by check-error (i.e. an `Error`
or similar).

This change basically checks strict equality on the matcher and the
thrown error first (such that `rejectedWith(undefined)` would work). It
then falls back to the original logic only if both the matcher and the
reason are truthy.

Fixes #263
  • Loading branch information
43081j authored May 13, 2024
1 parent 4b6fa17 commit 7e2b1a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/chai-as-promised.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ export default function (chai, utils) {
: checkError.compatibleConstructor(reason, errorLike));

const errMsgMatcherCompatible =
errMsgMatcher && checkError.compatibleMessage(reason, errMsgMatcher);
errMsgMatcher === reason ||
(errMsgMatcher &&
reason &&
checkError.compatibleMessage(reason, errMsgMatcher));

const reasonName = getReasonName(reason);

Expand Down
15 changes: 15 additions & 0 deletions test/assert-promise-specific.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,20 @@ describe('Assert interface:', () => {
});
});
});

describe('with undefined error', () => {
beforeEach(() => {
promise = Promise.reject();
});

describe('.isRejected(promise)', () => {
shouldPass(() => assert.isRejected(promise));
shouldPass(() => assert.isRejected(promise, undefined));
shouldFail({
op: () => assert.isRejected(promise, 'Error'),
message: "to be rejected with an error including 'Error' but got ''"
});
});
});
});
});

0 comments on commit 7e2b1a9

Please sign in to comment.