Skip to content

Commit

Permalink
Make makeRejectMatcher synchronizable (jestjs#5361)
Browse files Browse the repository at this point in the history
  • Loading branch information
incleaf committed Jan 22, 2018
1 parent 139f976 commit f2a068d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
12 changes: 12 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -37,6 +37,18 @@ describe('.rejects', () => {
await jestExpect(fn()).rejects.toThrow('some error');
});

[4, [1], {a: 1}, 'a', true, null, undefined, () => {}].forEach(value => {
it(`fails non-promise value ${stringify(value)} synchronously`, () => {
let error;
try {
jestExpect(value).rejects.toBe(111);
} catch (e) {
error = e;
}
expect(error).toBeDefined();
});
});

[4, [1], {a: 1}, 'a', true, null, undefined, () => {}].forEach(value => {
it(`fails non-promise value ${stringify(value)}`, async () => {
let error;
Expand Down
30 changes: 16 additions & 14 deletions packages/expect/src/index.js
Expand Up @@ -161,7 +161,7 @@ const makeRejectMatcher = (
matcher: RawMatcherFn,
isNot: boolean,
actual: Promise<any>,
): PromiseMatcherFn => async (...args) => {
): PromiseMatcherFn => (...args) => {
const matcherStatement = `.rejects.${isNot ? 'not.' : ''}${matcherName}`;
if (!isPromise(actual)) {
throw new JestAssertionError(
Expand All @@ -172,20 +172,22 @@ const makeRejectMatcher = (
);
}

let result;
try {
result = await actual;
} catch (e) {
return makeThrowingMatcher(matcher, isNot, e).apply(null, args);
}
return (async () => {
let result;
try {
result = await actual;
} catch (e) {
return makeThrowingMatcher(matcher, isNot, e).apply(null, args);
}

throw new JestAssertionError(
utils.matcherHint(matcherStatement, 'received', '') +
'\n\n' +
`Expected ${utils.RECEIVED_COLOR('received')} Promise to reject, ` +
'instead it resolved to value\n' +
` ${utils.printReceived(result)}`,
);
throw new JestAssertionError(
utils.matcherHint(matcherStatement, 'received', '') +
'\n\n' +
`Expected ${utils.RECEIVED_COLOR('received')} Promise to reject, ` +
'instead it resolved to value\n' +
` ${utils.printReceived(result)}`,
);
})();
};

const makeThrowingMatcher = (
Expand Down

0 comments on commit f2a068d

Please sign in to comment.