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

fix(calledWith) prevent falsy jestAsymmetricMatcher detection for Mock #75

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
4 changes: 2 additions & 2 deletions src/CalledWithFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ interface JestAsymmetricMatcher {
asymmetricMatch(...args: any[]): boolean;
}
function isJestAsymmetricMatcher(obj: any): obj is JestAsymmetricMatcher {
return !!obj && !!obj.asymmetricMatch && typeof obj.asymmetricMatch === 'function';
return !!obj && typeof obj === 'object' && 'asymmetricMatch' in obj && typeof obj.asymmetricMatch === 'function';
}

const checkCalledWith = <T, Y extends any[]>(calledWithStack: CalledWithStackItem<T, Y>[], actualArgs: Y): T => {
const calledWithInstance = calledWithStack.find((instance) =>
const calledWithInstance = calledWithStack.find(instance =>
instance.args.every((matcher, i) => {
if (matcher instanceof Matcher) {
return matcher.asymmetricMatch(actualArgs[i]);
Expand Down
13 changes: 13 additions & 0 deletions src/Mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface MockInt {
id: number;
someValue?: boolean | null;
getNumber: () => number;
getNumberWithMockArg: (mock: any) => number;
getSomethingWithArgs: (arg1: number, arg2: number) => number;
getSomethingWithMoreArgs: (arg1: number, arg2: number, arg3: number) => number;
}
Expand All @@ -29,6 +30,10 @@ class Test1 implements MockInt {
return this.id;
}

public getNumberWithMockArg(mock: any) {
return this.id;
}

public getSomethingWithArgs(arg1: number, arg2: number) {
return this.id;
}
Expand Down Expand Up @@ -214,6 +219,14 @@ describe('jest-mock-extended', () => {
expect(mockObj.getSomethingWithMoreArgs(1, 2, 3)).toBe(4);
expect(mockObj.getSomethingWithMoreArgs(1, 2, 4)).toBeUndefined;
});

test('Can use calledWith with an other mock', () => {
const mockObj = mock<MockInt>();
const mockArg = mock();
mockObj.getNumberWithMockArg.calledWith(mockArg).mockReturnValue(4);

expect(mockObj.getNumberWithMockArg(mockArg)).toBe(4);
})
});

describe('Matchers with toHaveBeenCalledWith', () => {
Expand Down