The aws-sdk-client-mock-jest package provides custom matchers to simplify verifying that commands were sent.
Installation:
npm install -D aws-sdk-client-mock-jest
Usage:
import 'aws-sdk-client-mock-jest';
// Verify a command was sent
expect(snsMock).toHaveReceivedCommand(PublishCommand);
// Verify command count
expect(snsMock).toHaveReceivedCommandTimes(PublishCommand, 2);
// Verify command with specific payload
expect(snsMock).toHaveReceivedCommandWith(PublishCommand, { Message: 'hello world' });
// Verify command with partial payload matching
expect(snsMock).toHaveReceivedCommandWith(PublishCommand, { Message: expect.stringContaining('hello') });
// Verify the Nth specific command
expect(snsMock).toHaveReceivedNthCommandWith(2, PublishCommand, { Message: 'hello world' });
Note: Shorter aliases like toReceiveCommandTimes() are also available.
import 'aws-sdk-client-mock-jest';
// a PublishCommand was sent to SNS
expect(snsMock).toHaveReceivedCommand(PublishCommand);
// at least one command was sent to SNS
expect(snsMock).toHaveReceivedAnyCommand();
// two PublishCommands were sent to SNS
expect(snsMock).toHaveReceivedCommandTimes(PublishCommand, 2);
// a PublishCommand with Message "hello world" was sent to SNS
expect(snsMock).toHaveReceivedCommandWith(
PublishCommand, {Message: 'hello world'}
);
// a PublishCommand with Message containing "hello" was sent to SNS
expect(snsMock).toHaveReceivedCommandWith(
PublishCommand, {Message: expect.stringContaining('hello')}
);
// the second PublishCommand sent to SNS was a PublishCommand with Message "hello world"
expect(snsMock).toHaveReceivedNthCommandWith(
2, PublishCommand, {Message: 'hello world'}
);
// the second PublishCommand sent to SNS had Message "hello world"
expect(snsMock).toHaveReceivedNthSpecificCommandWith(
2, PublishCommand, {Message: 'hello world'}
);