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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 an async function

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 an async function

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 an async function

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 an async function

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 an async function

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 an async function

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 an async function

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 an async function

Received has value: <r>undefined</>
`;
Expand Down
7 changes: 5 additions & 2 deletions packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -55,7 +55,11 @@ 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 Expand Up @@ -101,7 +105,6 @@ describe('.rejects', () => {
expect(error.message).toMatchSnapshot();
});
});

it('fails for promise that resolves', async () => {
let error;
try {
Expand Down
37 changes: 25 additions & 12 deletions packages/expect/src/index.ts
Expand Up @@ -9,6 +9,7 @@
import * as matcherUtils from 'jest-matcher-utils';
import type {
AsyncExpectationResult,
AsyncFunction,
Expect,
ExpectationResult,
MatcherState as JestMatcherState,
Expand Down Expand Up @@ -57,6 +58,9 @@ const isPromise = <T extends any>(obj: any): obj is PromiseLike<T> =>
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function';

const isAsyncFunction = (fn: any): boolean =>
!!fn && fn.constructor && fn.constructor.name == 'AsyncFunction';

const createToThrowErrorMatchingSnapshotMatcher = function (
matcher: RawMatcherFn,
) {
Expand Down Expand Up @@ -189,31 +193,40 @@ const makeRejectMatcher = (
matcherName: string,
matcher: RawMatcherFn,
isNot: boolean,
actual: Promise<any>,
actual: Promise<any> | AsyncFunction,
outerErr: JestAssertionError,
): PromiseMatcherFn => (...args) => {
const options = {
isNot,
promise: 'rejects',
};
let actualWrapper: Promise<any>;

if (!isPromise(actual)) {
throw new JestAssertionError(
matcherUtils.matcherErrorMessage(
matcherUtils.matcherHint(matcherName, undefined, '', options),
`${matcherUtils.RECEIVED_COLOR('received')} value must be a promise`,
matcherUtils.printWithType(
'Received',
actual,
matcherUtils.printReceived,
if (!isAsyncFunction(actual)) {
throw new JestAssertionError(
matcherUtils.matcherErrorMessage(
matcherUtils.matcherHint(matcherName, undefined, '', options),
`${matcherUtils.RECEIVED_COLOR(
'received',
)} value must be a promise or an async function`,
matcherUtils.printWithType(
'Received',
actual,
matcherUtils.printReceived,
),
),
),
);
);
} else {
actualWrapper = Promise.resolve().then(actual);
}
} else {
actualWrapper = actual;
}

const innerErr = new JestAssertionError();

return actual.then(
return actualWrapper.then(
result => {
outerErr.message =
matcherUtils.matcherHint(matcherName, undefined, '', options) +
Expand Down
2 changes: 2 additions & 0 deletions packages/expect/src/types.ts
Expand Up @@ -16,6 +16,8 @@ export type SyncExpectationResult = {

export type AsyncExpectationResult = Promise<SyncExpectationResult>;

export type AsyncFunction = (...args: Array<any>) => Promise<any>;

export type ExpectationResult = SyncExpectationResult | AsyncExpectationResult;

export type RawMatcherFn = {
Expand Down