From 6f7bb5940ed3f568f54e7d8800520ea1be9641a3 Mon Sep 17 00:00:00 2001 From: Maciej Radzikowski Date: Mon, 1 Jan 2024 22:26:13 +0100 Subject: [PATCH] feat(jest): fail if more than expected matcher arguments are passed (#199) * feat(jest): fail if more than expected matcher arguments are passed Prevents accidental calling toHaveReceivedCommand(Cmd, {}) instead of toHaveReceivedCommandWith(Cmd, {}) Closes #191 --- .../aws-sdk-client-mock-jest/src/jestMatchers.ts | 16 +++++++++++++++- .../test/jestMatchers.test.ts | 9 +++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/aws-sdk-client-mock-jest/src/jestMatchers.ts b/packages/aws-sdk-client-mock-jest/src/jestMatchers.ts index bf39fcf..3e86546 100644 --- a/packages/aws-sdk-client-mock-jest/src/jestMatchers.ts +++ b/packages/aws-sdk-client-mock-jest/src/jestMatchers.ts @@ -3,8 +3,8 @@ import assert from 'assert'; import type {MetadataBearer} from '@smithy/types'; import {AwsCommand, AwsStub} from 'aws-sdk-client-mock'; import type {SinonSpyCall} from 'sinon'; -import {expect} from 'expect'; import type {ExpectationResult, MatcherContext, MatcherFunction} from 'expect'; +import {expect} from 'expect'; interface AwsSdkJestMockBaseMatchers extends Record { /** @@ -254,6 +254,10 @@ const processMatch = ({ctx, mockClient, command, check, m return {pass, message: msg}; }; +const ensureNoOtherArgs = (args: unknown[]): void => { + assert(args.length === 0, 'Too many matcher arguments'); +}; + const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers]: MatcherFunction } = { /** * implementation of {@link AwsSdkJestMockMatchers.toHaveReceivedCommand} matcher @@ -262,7 +266,9 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers]: MatcherF this: MatcherContext, mockClient: unknown, command: new () => AnyCommand, + ...other: unknown[] ) { + ensureNoOtherArgs(other); return processMatch({ ctx: this, mockClient, @@ -282,7 +288,9 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers]: MatcherF mockClient: unknown, command: new () => AnyCommand, expectedCalls: number, + ...other: unknown[] ) { + ensureNoOtherArgs(other); return processMatch({ ctx: this, mockClient, @@ -302,7 +310,9 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers]: MatcherF mockClient: unknown, command: new () => AnyCommand, input: Record, + ...other: unknown[] ) { + ensureNoOtherArgs(other); return processMatch<{ matchCount: number }>({ ctx: this, mockClient, @@ -339,7 +349,9 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers]: MatcherF call: number, command: new () => AnyCommand, input: Record, + ...other: unknown[] ) { + ensureNoOtherArgs(other); assert( call && typeof call === 'number' && call > 0, 'Call number must be a number greater than 0', @@ -398,7 +410,9 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers]: MatcherF call: number, command: new () => AnyCommand, input: Record, + ...other: unknown[] ) { + ensureNoOtherArgs(other); assert( call && typeof call === 'number' && call > 0, 'Call number must be a number greater than 0', diff --git a/packages/aws-sdk-client-mock-jest/test/jestMatchers.test.ts b/packages/aws-sdk-client-mock-jest/test/jestMatchers.test.ts index 75e57db..e7d7ee5 100644 --- a/packages/aws-sdk-client-mock-jest/test/jestMatchers.test.ts +++ b/packages/aws-sdk-client-mock-jest/test/jestMatchers.test.ts @@ -40,6 +40,15 @@ Calls: 1. PublishCommand: {"Message": "mock message", "TopicArn": "arn:aws:sns:us-east-1:111111111111:MyTopic"}" `); }); + + it('fails on more arguments', async () => { + const sns = new SNSClient({}); + await sns.send(publishCmd1); + + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + expect(() => expect(snsMock).not.toHaveReceivedCommand(PublishCommand, {})).toThrowErrorMatchingInlineSnapshot('"Too many matcher arguments"'); + }); }); describe('toHaveReceivedCommandTimes', () => {