Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support async functions in toThrow #9817

Merged
merged 6 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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