aws-sdk-mock is a JavaScript/TypeScript library designed to facilitate unit testing of applications that interact with the AWS SDK, specifically targeting AWS SDK for JavaScript v2. It enables developers to replace actual AWS service calls with predefined responses or custom functions during tests, preventing unintended interactions with real AWS infrastructure. The current stable version is 6.2.2. While there isn't a strict time-based release cadence, the project is actively maintained, with recent minor updates addressing dependencies and security vulnerabilities (e.g., v6.2.0 addressing CVE-2024-45296) and a significant TypeScript rewrite in v6.0.1. It differentiates itself by leveraging Sinon.js internally to create robust mocks for AWS service methods like S3.getObject or DynamoDB.putItem, offering explicit `mock`, `restore`, and `remock` functions. A key limitation and differentiator is its primary focus on AWS SDK v2; for AWS SDK v3, direct mocking of modular clients might be preferred, reducing the need for this library.
npm install aws-sdk-mockVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to mock an AWS SDK v2 S3 client's `getObject` method, execute a call, and then restore the original method.
If using AWS SDK v3, refactor your tests to use standard mocking patterns directly on the modular v3 clients or explore v3-specific mocking solutions. Do not attempt to use aws-sdk-mock with v3 clients.
Review type declarations in your project if upgrading from pre-v6 to ensure compatibility. Re-check any custom type assertions or extensions related to aws-sdk-mock.
Upgrade to aws-sdk-mock v6.2.0 or later to ensure you have the security fix.
Always use named imports: `import { mock, restore, remock } from 'aws-sdk-mock';` or CommonJS `const { mock, restore, remock } = require('aws-sdk-mock');`Install the AWS SDK v2: `npm install aws-sdk@2` or `yarn add aws-sdk@2`.
Use a named import: `import { mock, restore } from 'aws-sdk-mock';`Ensure you are using AWS SDK v2. Verify the service name (e.g., 'S3', 'DynamoDB') and method name (e.g., 'putObject', 'getItem') are correct and match the v2 API. Confirm that `aws-sdk` is imported and used in the code you are testing.