Skip to content

Commit

Permalink
Add support for toHaveReceivedAnyCommand matcher
Browse files Browse the repository at this point in the history
Signed-off-by: Yaron Yarimi <yaron.yarimi@env0.com>
  • Loading branch information
yaronya committed Jan 4, 2024
1 parent 9d930da commit a7ec547
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
40 changes: 36 additions & 4 deletions packages/aws-sdk-client-mock-jest/src/jestMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ interface AwsSdkJestMockBaseMatchers<R> extends Record<string, Function> {
command: new (input: TCmdInput) => AwsCommand<TCmdInput, TCmdOutput>,
input: Partial<TCmdInput>,
): R;

/**
* Asserts {@link AwsStub Aws Client Mock} received any command
*/
toHaveReceivedAnyCommand(): R;
}

interface AwsSdkJestMockAliasMatchers<R> extends Record<string, Function> {
Expand Down Expand Up @@ -138,6 +143,11 @@ interface AwsSdkJestMockAliasMatchers<R> extends Record<string, Function> {
command: new (input: TCmdInput) => AwsCommand<TCmdInput, TCmdOutput>,
input: Partial<TCmdInput>,
): R;

/**
* Asserts {@link AwsStub Aws Client Mock} received any command
*/
toReceiveAnyCommand(): R;
}

/**
Expand Down Expand Up @@ -191,6 +201,7 @@ type MessageFunctionParams<CheckData> = {
cmd: string;
client: string;
commandCalls: AnySpyCall[];
calls: AnySpyCall[];
data: CheckData;
notPrefix: string;
};
Expand All @@ -214,15 +225,15 @@ const printCalls = (ctx: MatcherContext, calls: AnySpyCall[]): string[] =>
const processMatch = <CheckData = undefined>({ctx, mockClient, command, check, message}: {
ctx: MatcherContext;
mockClient: unknown;
command: new () => AnyCommand;
command?: new () => AnyCommand;
check: (params: { calls: AnySpyCall[]; commandCalls: AnySpyCall[] }) => {
pass: boolean;
data: CheckData;
};
message: (params: MessageFunctionParams<CheckData>) => string[];
}): ExpectationResult => {
assert(mockClient instanceof AwsStub, 'The actual must be a client mock instance');
assert(
command && assert(
command &&
typeof command === 'function' &&
typeof command.name === 'string' &&
Expand All @@ -231,19 +242,20 @@ const processMatch = <CheckData = undefined>({ctx, mockClient, command, check, m
);

const calls = mockClient.calls();
const commandCalls = mockClient.commandCalls(command);
const commandCalls = command ? mockClient.commandCalls(command) : [];

const {pass, data} = check({calls, commandCalls});

const msg = (): string => {
const cmd = ctx.utils.printExpected(command.name);
const cmd = ctx.utils.printExpected(command?.name || 'Any Command');
const client = mockClient.clientName();

return [
...message({
client,
cmd,
data,
calls,
commandCalls,
notPrefix: ctx.isNot ? 'not ' : '',
}),
Expand Down Expand Up @@ -462,6 +474,25 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers<unknown>]: MatcherF
],
});
},
/**
* implementation of {@link AwsSdkJestMockMatchers.toHaveReceivedAnyCommand} matcher
*/
toHaveReceivedAnyCommand(
this: MatcherContext,
mockClient: unknown,
...other: unknown[]
) {
ensureNoOtherArgs(other);
return processMatch({
ctx: this,
mockClient,
check: ({calls}) => ({pass: calls.length > 0, data: undefined}),
message: ({client, notPrefix, commandCalls}) => [
`Expected ${client} to ${notPrefix}receive any command`,
`${client} received any command ${this.utils.printReceived(commandCalls.length)} times`,
],
});
}
};

/* typing ensures keys matching */
Expand All @@ -471,6 +502,7 @@ const aliasMatchers: { [P in keyof AwsSdkJestMockAliasMatchers<unknown>]: Matche
toReceiveCommandWith: baseMatchers.toHaveReceivedCommandWith,
toReceiveNthCommandWith: baseMatchers.toHaveReceivedNthCommandWith,
toReceiveNthSpecificCommandWith: baseMatchers.toHaveReceivedNthSpecificCommandWith,
toReceiveAnyCommand: baseMatchers.toHaveReceivedAnyCommand
};

// Skip registration if jest expect does not exist
Expand Down
20 changes: 20 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 @@ -273,3 +273,23 @@ Calls:
`);
});
});

describe('toHaveReceivedAnyCommand', () => {
it.each`
command
${publishCmd1}
${subscribeCmd1}
`('passes on receiving any command', async ({ command }) => {
const sns = new SNSClient({});
await sns.send(command);

expect(() => expect(snsMock).toHaveReceivedAnyCommand()).not.toThrow();
})

it('fails on not receiving any command', () => {
expect(() => expect(snsMock).toHaveReceivedAnyCommand()).toThrowErrorMatchingInlineSnapshot(`
"Expected SNSClient to receive any command
SNSClient received any command <red>0</color> times"
`);
});
})

0 comments on commit a7ec547

Please sign in to comment.