Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reorganize code using promise instaed of async/await
  • Loading branch information
Hyeonsu Lee committed Jan 23, 2018
commit a5cdc1050251c7f0d1417d45ae86eb1eecaf85c9
28 changes: 12 additions & 16 deletions packages/expect/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,18 @@ const makeRejectMatcher = (
);
}

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)}`,
);
})();
return actual
.then(v => {
const err = new JestAssertionError(
utils.matcherHint(matcherStatement, 'received', '') +
'\n\n' +
`Expected ${utils.RECEIVED_COLOR('received')} Promise to reject, ` +
'instead it resolved to value\n' +
` ${utils.printReceived(v)}`,
);
return Promise.reject(err);
})
.catch(e => makeThrowingMatcher(matcher, isNot, e).apply(null, args));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This catch has to be first

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait no, that won't work either. You have to use .then(resolvedHandler, rejectedHandler) to avoid handling the rejection here.

So just move your catch into the second argument of the then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://repl.it/repls/RevolvingEvergreenBlackbuck
Is there any difference between A and B, besides syntax preference? Both a and b pass the test cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, yes. since we expect it to fail we need to have our catch first so we can resolve the promise with it. But if it does not reject we need to reject it ourselves.

If we add a then after the catch, it's not called. If we add our catch onto a then, we locally catch the rejection we want to bubble up

};

const makeThrowingMatcher = (
Expand Down