Skip to content

Commit

Permalink
feat: support async functions in toThrow (#9817)
Browse files Browse the repository at this point in the history
  • Loading branch information
notoriousmango committed Apr 16, 2020
1 parent 4010fdd commit fcfe7cd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### Features

- `[expect]` Support `async function`s in `toThrow` ([#9817](https://github.com/facebook/jest/pull/9817))
- `[jest-console]` Add code frame to `console.error` and `console.warn` ([#9741](https://github.com/facebook/jest/pull/9741))
- `[@jest/globals]` New package so Jest's globals can be explicitly imported ([#9801](https://github.com/facebook/jest/pull/9801))

Expand Down
Expand Up @@ -10,7 +10,7 @@ Resolved to value: <r>4</>
exports[`.rejects fails non-promise value "a" 1`] = `
<d>expect(</><r>received</><d>).</>rejects<d>.</>toBeDefined<d>()</>

<b>Matcher error</>: <r>received</> value must be a promise
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise

Received has type: string
Received has value: <r>"a"</>
Expand All @@ -19,7 +19,7 @@ Received has value: <r>"a"</>
exports[`.rejects fails non-promise value [1] 1`] = `
<d>expect(</><r>received</><d>).</>rejects<d>.</>toBeDefined<d>()</>

<b>Matcher error</>: <r>received</> value must be a promise
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise

Received has type: array
Received has value: <r>[1]</>
Expand All @@ -28,7 +28,7 @@ Received has value: <r>[1]</>
exports[`.rejects fails non-promise value [Function anonymous] 1`] = `
<d>expect(</><r>received</><d>).</>rejects<d>.</>toBeDefined<d>()</>

<b>Matcher error</>: <r>received</> value must be a promise
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise

Received has type: function
Received has value: <r>[Function anonymous]</>
Expand All @@ -37,7 +37,7 @@ Received has value: <r>[Function anonymous]</>
exports[`.rejects fails non-promise value {"a": 1} 1`] = `
<d>expect(</><r>received</><d>).</>rejects<d>.</>toBeDefined<d>()</>

<b>Matcher error</>: <r>received</> value must be a promise
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise

Received has type: object
Received has value: <r>{"a": 1}</>
Expand All @@ -46,7 +46,7 @@ Received has value: <r>{"a": 1}</>
exports[`.rejects fails non-promise value 4 1`] = `
<d>expect(</><r>received</><d>).</>rejects<d>.</>not<d>.</>toBeDefined<d>()</>

<b>Matcher error</>: <r>received</> value must be a promise
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise

Received has type: number
Received has value: <r>4</>
Expand All @@ -55,15 +55,15 @@ Received has value: <r>4</>
exports[`.rejects fails non-promise value null 1`] = `
<d>expect(</><r>received</><d>).</>rejects<d>.</>not<d>.</>toBeDefined<d>()</>

<b>Matcher error</>: <r>received</> value must be a promise
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise

Received has value: <r>null</>
`;

exports[`.rejects fails non-promise value true 1`] = `
<d>expect(</><r>received</><d>).</>rejects<d>.</>not<d>.</>toBeDefined<d>()</>

<b>Matcher error</>: <r>received</> value must be a promise
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise

Received has type: boolean
Received has value: <r>true</>
Expand All @@ -72,7 +72,7 @@ Received has value: <r>true</>
exports[`.rejects fails non-promise value undefined 1`] = `
<d>expect(</><r>received</><d>).</>rejects<d>.</>not<d>.</>toBeDefined<d>()</>

<b>Matcher error</>: <r>received</> value must be a promise
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise

Received has value: <r>undefined</>
`;
Expand Down
6 changes: 6 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -56,6 +56,12 @@ describe('.rejects', () => {
await jestExpect(fn()).rejects.toThrow('some error');
});

it('should reject async function to toThrow', async () => {
await expect(async () => {
throw new Error('Test');
}).rejects.toThrow('Test');
});

['a', [1], () => {}, {a: 1}].forEach(value => {
it(`fails non-promise value ${stringify(value)} synchronously`, () => {
let error;
Expand Down
13 changes: 9 additions & 4 deletions packages/expect/src/index.ts
Expand Up @@ -189,19 +189,24 @@ const makeRejectMatcher = (
matcherName: string,
matcher: RawMatcherFn,
isNot: boolean,
actual: Promise<any>,
actual: Promise<any> | (() => Promise<any>),
outerErr: JestAssertionError,
): PromiseMatcherFn => (...args) => {
const options = {
isNot,
promise: 'rejects',
};

if (!isPromise(actual)) {
const actualWrapper: Promise<any> =
typeof actual === 'function' ? actual() : actual;

if (!isPromise(actualWrapper)) {
throw new JestAssertionError(
matcherUtils.matcherErrorMessage(
matcherUtils.matcherHint(matcherName, undefined, '', options),
`${matcherUtils.RECEIVED_COLOR('received')} value must be a promise`,
`${matcherUtils.RECEIVED_COLOR(
'received',
)} value must be a promise or a function returning a promise`,
matcherUtils.printWithType(
'Received',
actual,
Expand All @@ -213,7 +218,7 @@ const makeRejectMatcher = (

const innerErr = new JestAssertionError();

return actual.then(
return actualWrapper.then(
result => {
outerErr.message =
matcherUtils.matcherHint(matcherName, undefined, '', options) +
Expand Down

0 comments on commit fcfe7cd

Please sign in to comment.