Skip to content

Commit 7e2b1a9

Browse files
authored
fix: raise a nicer error when non-object errors are matched (#294)
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
1 parent 4b6fa17 commit 7e2b1a9

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/chai-as-promised.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ export default function (chai, utils) {
215215
: checkError.compatibleConstructor(reason, errorLike));
216216

217217
const errMsgMatcherCompatible =
218-
errMsgMatcher && checkError.compatibleMessage(reason, errMsgMatcher);
218+
errMsgMatcher === reason ||
219+
(errMsgMatcher &&
220+
reason &&
221+
checkError.compatibleMessage(reason, errMsgMatcher));
219222

220223
const reasonName = getReasonName(reason);
221224

test/assert-promise-specific.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,20 @@ describe('Assert interface:', () => {
289289
});
290290
});
291291
});
292+
293+
describe('with undefined error', () => {
294+
beforeEach(() => {
295+
promise = Promise.reject();
296+
});
297+
298+
describe('.isRejected(promise)', () => {
299+
shouldPass(() => assert.isRejected(promise));
300+
shouldPass(() => assert.isRejected(promise, undefined));
301+
shouldFail({
302+
op: () => assert.isRejected(promise, 'Error'),
303+
message: "to be rejected with an error including 'Error' but got ''"
304+
});
305+
});
306+
});
292307
});
293308
});

0 commit comments

Comments
 (0)