aws-sdk-client-mock-jest provides custom Jest matchers that extend Jest's `expect` API, specifically designed to simplify testing AWS SDK v3 clients when used in conjunction with `aws-sdk-client-mock`. It enables developers to assert on AWS SDK command invocations with readable and intuitive syntax like `toHaveReceivedCommand` or `toHaveReceivedCommandWith`. The package is currently at version 4.1.0 and maintains an active release cadence, with several minor and patch releases in recent months, often including beta cycles. Its key differentiator is the seamless integration into Jest's testing framework, allowing for robust unit and integration tests of AWS Lambda functions, frontend applications, and Node.js services interacting with AWS, without needing to manually inspect mock call arguments. Version 4.1.0 notably introduced support for Vitest, broadening its compatibility within the JavaScript testing ecosystem.
npm install aws-sdk-client-mock-jestVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to set up `aws-sdk-client-mock` with Jest matchers to test DynamoDB `PutItemCommand` invocations, asserting on command types, call counts, and input parameters.
Review tests using `toHaveReceivedCommandWith` and adjust `expect.assertions()` counts if necessary to reflect the correct number of assertions made by the matcher.
Upgrade to `aws-sdk-client-mock-jest@4.0.2` or higher to ensure robust partial matching of `Command` input objects, allowing you to specify only the relevant fields for assertion.
Update to `aws-sdk-client-mock-jest@4.0.1` or newer to correctly utilize `expect.any`, `expect.objectContaining`, and other asymmetric matchers within `toHaveReceivedCommandWith` assertions.
Verify that your `package.json` correctly reflects `vitest` as an optional peer dependency if you intend to use it, and that your test runner configuration (`jest.config.js` or `vitest.config.ts`) is set up to handle the matchers appropriately for the chosen framework.
Add `import 'aws-sdk-client-mock-jest';` to your test setup file (e.g., `setupFilesAfterEnv` in Jest configuration) or directly at the top of your test files.
Ensure that `expect()` is called with the result of `mockClient(YourClient)` (e.g., `expect(ddbMock)`) not the client instance itself (e.g., `expect(ddbClient)`).
Review your application code to confirm the correct AWS SDK command is being sent, or adjust your test assertion to match the actual command type invoked.
Inspect the input arguments passed to your AWS SDK command in the application code and ensure they precisely match (or are a partial match of) the object provided to `toHaveReceivedCommandWith`.