Skip to content

Commit

Permalink
feat(jest): fail if more than expected matcher arguments are passed (#…
Browse files Browse the repository at this point in the history
…199)

* feat(jest): fail if more than expected matcher arguments are passed

Prevents accidental calling toHaveReceivedCommand(Cmd, {})
instead of toHaveReceivedCommandWith(Cmd, {})

Closes #191
  • Loading branch information
m-radzikowski authored Jan 1, 2024
1 parent 8f14cbb commit 6f7bb59
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/aws-sdk-client-mock-jest/src/jestMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<R> extends Record<string, Function> {
/**
Expand Down Expand Up @@ -254,6 +254,10 @@ const processMatch = <CheckData = undefined>({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<unknown>]: MatcherFunction<any[]> } = {
/**
* implementation of {@link AwsSdkJestMockMatchers.toHaveReceivedCommand} matcher
Expand All @@ -262,7 +266,9 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers<unknown>]: MatcherF
this: MatcherContext,
mockClient: unknown,
command: new () => AnyCommand,
...other: unknown[]
) {
ensureNoOtherArgs(other);
return processMatch({
ctx: this,
mockClient,
Expand All @@ -282,7 +288,9 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers<unknown>]: MatcherF
mockClient: unknown,
command: new () => AnyCommand,
expectedCalls: number,
...other: unknown[]
) {
ensureNoOtherArgs(other);
return processMatch({
ctx: this,
mockClient,
Expand All @@ -302,7 +310,9 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers<unknown>]: MatcherF
mockClient: unknown,
command: new () => AnyCommand,
input: Record<string, unknown>,
...other: unknown[]
) {
ensureNoOtherArgs(other);
return processMatch<{ matchCount: number }>({
ctx: this,
mockClient,
Expand Down Expand Up @@ -339,7 +349,9 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers<unknown>]: MatcherF
call: number,
command: new () => AnyCommand,
input: Record<string, unknown>,
...other: unknown[]
) {
ensureNoOtherArgs(other);
assert(
call && typeof call === 'number' && call > 0,
'Call number must be a number greater than 0',
Expand Down Expand Up @@ -398,7 +410,9 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers<unknown>]: MatcherF
call: number,
command: new () => AnyCommand,
input: Record<string, unknown>,
...other: unknown[]
) {
ensureNoOtherArgs(other);
assert(
call && typeof call === 'number' && call > 0,
'Call number must be a number greater than 0',
Expand Down
9 changes: 9 additions & 0 deletions packages/aws-sdk-client-mock-jest/test/jestMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ Calls:
1. PublishCommand: <red>{"Message": "mock message", "TopicArn": "arn:aws:sns:us-east-1:111111111111:MyTopic"}</color>"
`);
});

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', () => {
Expand Down

0 comments on commit 6f7bb59

Please sign in to comment.