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

chore(expect): type spy and throwing matchers correctly #13511

Merged
merged 1 commit into from
Oct 26, 2022
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
chore(expect): type spy and throwing matchers correctly
  • Loading branch information
SimenB committed Oct 25, 2022
commit 0703bc02026823890279ff28d5c91076fe59d136
104 changes: 41 additions & 63 deletions packages/expect/src/spyMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
stringify,
} from 'jest-matcher-utils';
import type {
MatcherState,
MatcherFunction,
MatchersObject,
SyncExpectationResult,
} from './types';
Expand Down Expand Up @@ -353,12 +353,10 @@ const printReceivedResults = (
);
};

const createToBeCalledMatcher = (matcherName: string) =>
function (
this: MatcherState,
received: any,
expected: unknown,
): SyncExpectationResult {
const createToBeCalledMatcher = (
matcherName: string,
): MatcherFunction<[unknown]> =>
function (received: any, expected: unknown): SyncExpectationResult {
const expectedArgument = '';
const options: MatcherHintOptions = {
isNot: this.isNot,
Expand Down Expand Up @@ -402,12 +400,10 @@ const createToBeCalledMatcher = (matcherName: string) =>
return {message, pass};
};

const createToReturnMatcher = (matcherName: string) =>
function (
this: MatcherState,
received: any,
expected: unknown,
): SyncExpectationResult {
const createToReturnMatcher = (
matcherName: string,
): MatcherFunction<[unknown]> =>
function (received: any, expected): SyncExpectationResult {
const expectedArgument = '';
const options: MatcherHintOptions = {
isNot: this.isNot,
Expand Down Expand Up @@ -462,12 +458,10 @@ const createToReturnMatcher = (matcherName: string) =>
return {message, pass};
};

const createToBeCalledTimesMatcher = (matcherName: string) =>
function (
this: MatcherState,
received: any,
expected: number,
): SyncExpectationResult {
const createToBeCalledTimesMatcher = (
matcherName: string,
): MatcherFunction<[number]> =>
function (received: any, expected): SyncExpectationResult {
const expectedArgument = 'expected';
const options: MatcherHintOptions = {
isNot: this.isNot,
Expand Down Expand Up @@ -500,12 +494,10 @@ const createToBeCalledTimesMatcher = (matcherName: string) =>
return {message, pass};
};

const createToReturnTimesMatcher = (matcherName: string) =>
function (
this: MatcherState,
received: any,
expected: number,
): SyncExpectationResult {
const createToReturnTimesMatcher = (
matcherName: string,
): MatcherFunction<[number]> =>
function (received: any, expected): SyncExpectationResult {
const expectedArgument = 'expected';
const options: MatcherHintOptions = {
isNot: this.isNot,
Expand Down Expand Up @@ -550,12 +542,10 @@ const createToReturnTimesMatcher = (matcherName: string) =>
return {message, pass};
};

const createToBeCalledWithMatcher = (matcherName: string) =>
function (
this: MatcherState,
received: any,
...expected: Array<unknown>
): SyncExpectationResult {
const createToBeCalledWithMatcher = (
matcherName: string,
): MatcherFunction<Array<unknown>> =>
function (received: any, ...expected): SyncExpectationResult {
const expectedArgument = '...expected';
const options: MatcherHintOptions = {
isNot: this.isNot,
Expand Down Expand Up @@ -625,12 +615,10 @@ const createToBeCalledWithMatcher = (matcherName: string) =>
return {message, pass};
};

const createToReturnWithMatcher = (matcherName: string) =>
function (
this: MatcherState,
received: any,
expected: unknown,
): SyncExpectationResult {
const createToReturnWithMatcher = (
matcherName: string,
): MatcherFunction<[unknown]> =>
function (received: any, expected): SyncExpectationResult {
const expectedArgument = 'expected';
const options: MatcherHintOptions = {
isNot: this.isNot,
Expand Down Expand Up @@ -700,12 +688,10 @@ const createToReturnWithMatcher = (matcherName: string) =>
return {message, pass};
};

const createLastCalledWithMatcher = (matcherName: string) =>
function (
this: MatcherState,
received: any,
...expected: Array<unknown>
): SyncExpectationResult {
const createLastCalledWithMatcher = (
matcherName: string,
): MatcherFunction<Array<unknown>> =>
function (received: any, ...expected): SyncExpectationResult {
const expectedArgument = '...expected';
const options: MatcherHintOptions = {
isNot: this.isNot,
Expand Down Expand Up @@ -785,12 +771,10 @@ const createLastCalledWithMatcher = (matcherName: string) =>
return {message, pass};
};

const createLastReturnedMatcher = (matcherName: string) =>
function (
this: MatcherState,
received: any,
expected: unknown,
): SyncExpectationResult {
const createLastReturnedMatcher = (
matcherName: string,
): MatcherFunction<[unknown]> =>
function (received: any, expected): SyncExpectationResult {
const expectedArgument = 'expected';
const options: MatcherHintOptions = {
isNot: this.isNot,
Expand Down Expand Up @@ -871,13 +855,10 @@ const createLastReturnedMatcher = (matcherName: string) =>
return {message, pass};
};

const createNthCalledWithMatcher = (matcherName: string) =>
function (
this: MatcherState,
received: any,
nth: number,
...expected: Array<unknown>
): SyncExpectationResult {
const createNthCalledWithMatcher = (
matcherName: string,
): MatcherFunction<[number, ...Array<unknown>]> =>
function (received: any, nth, ...expected): SyncExpectationResult {
const expectedArgument = 'n';
const options: MatcherHintOptions = {
expectedColor: (arg: string) => arg,
Expand Down Expand Up @@ -1003,13 +984,10 @@ const createNthCalledWithMatcher = (matcherName: string) =>
return {message, pass};
};

const createNthReturnedWithMatcher = (matcherName: string) =>
function (
this: MatcherState,
received: any,
nth: number,
expected: unknown,
): SyncExpectationResult {
const createNthReturnedWithMatcher = (
matcherName: string,
): MatcherFunction<[number, unknown]> =>
function (received: any, nth, expected): SyncExpectationResult {
const expectedArgument = 'n';
const options: MatcherHintOptions = {
expectedColor: (arg: string) => arg,
Expand Down
11 changes: 3 additions & 8 deletions packages/expect/src/toThrowMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ import {
} from './print';
import type {
ExpectationResult,
MatcherState,
MatcherFunction,
MatchersObject,
RawMatcherFn,
SyncExpectationResult,
} from './types';

Expand Down Expand Up @@ -77,12 +76,8 @@ const getThrown = (e: any): Thrown => {
export const createMatcher = (
matcherName: string,
fromPromise?: boolean,
): RawMatcherFn =>
function (
this: MatcherState,
received: Function,
expected: any,
): ExpectationResult {
): MatcherFunction<[any]> =>
function (received, expected): ExpectationResult {
const options = {
isNot: this.isNot,
promise: this.promise,
Expand Down