{"id":12713,"library":"aws-sdk-mock","title":"AWS SDK Mocking Library for JavaScript v2","description":"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.","status":"maintenance","version":"6.2.2","language":"javascript","source_language":"en","source_url":"https://github.com/dwyl/aws-sdk-mock","tags":["javascript","aws-sdk","aws","Amazon","Lambda","API-Gateway","S3","DynamoDB","SNS","typescript"],"install":[{"cmd":"npm install aws-sdk-mock","lang":"bash","label":"npm"},{"cmd":"yarn add aws-sdk-mock","lang":"bash","label":"yarn"},{"cmd":"pnpm add aws-sdk-mock","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This library mocks methods on 'aws-sdk' v2 clients. It expects 'aws-sdk' to be installed as a peer dependency.","package":"aws-sdk","optional":false},{"reason":"Used internally for mocking functionality.","package":"sinon","optional":false}],"imports":[{"note":"The library primarily uses named exports. The README's 'AWS.mock(...)' notation is a common pattern but not a direct import style for the functions themselves.","wrong":"import AWSMock from 'aws-sdk-mock';\nAWSMock.mock(...);","symbol":"mock","correct":"import { mock } from 'aws-sdk-mock';"},{"note":"Used to revert mocked AWS service methods to their original implementations. It's crucial for cleaning up tests.","wrong":"const { restore } = require('aws-sdk-mock'); // For CommonJS\n// Or incorrect usage like AWSMock.restore(...);","symbol":"restore","correct":"import { restore } from 'aws-sdk-mock';"},{"note":"Allows updating the replacement function for an already existing mock without needing to restore and re-mock, useful in multi-step tests.","wrong":"import AWS from 'aws-sdk-mock';\nAWS.remock(...);","symbol":"remock","correct":"import { remock } from 'aws-sdk-mock';"}],"quickstart":{"code":"import { S3 } from 'aws-sdk';\nimport { mock, restore } from 'aws-sdk-mock';\n\n// Ensure aws-sdk@2 is installed (e.g., `npm i aws-sdk@2`)\n// This example uses an async IIFE for demonstration.\n\n(async () => {\n  const bucket = 'my-mock-bucket';\n  const key = 'test-file.txt';\n  const mockData = { Body: Buffer.from('Mocked content!') };\n\n  // 1. Mock an S3 service method\n  mock('S3', 'getObject', (params, callback) => {\n    console.log(`Mocking S3.getObject for ${params.Bucket}/${params.Key}`);\n    callback(null, mockData);\n  });\n\n  // 2. Use the AWS SDK client as usual\n  const s3Client = new S3();\n  const result = await s3Client.getObject({ Bucket: bucket, Key: key }).promise();\n\n  console.log('Received mocked data:', result.Body?.toString()); // Expected: 'Mocked content!'\n\n  // 3. Restore the original method after the test\n  restore('S3', 'getObject');\n\n  console.log('Mock restored. Subsequent calls would use original S3 or fail without credentials.');\n})();","lang":"typescript","description":"Demonstrates how to mock an AWS SDK v2 S3 client's `getObject` method, execute a call, and then restore the original method."},"warnings":[{"fix":"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.","message":"aws-sdk-mock is primarily designed for AWS SDK for JavaScript v2. It is NOT recommended for use with AWS SDK v3 (modular SDK). For v3, consider direct mocking techniques or purpose-built v3 mocking libraries.","severity":"breaking","affected_versions":">=5.x"},{"fix":"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.","message":"Version 6.0.1 included a complete TypeScript rewrite of the library. While the core API remained largely similar for JavaScript users, this introduced changes to type definitions and internal structure, potentially impacting existing TypeScript projects.","severity":"breaking","affected_versions":">=6.0.1"},{"fix":"Upgrade to aws-sdk-mock v6.2.0 or later to ensure you have the security fix.","message":"A minor maintenance release (v6.2.0) updated dependencies to address a security vulnerability (CVE-2024-45296) in one of its transitive dependencies.","severity":"gotcha","affected_versions":"<6.2.0"},{"fix":"Always use named imports: `import { mock, restore, remock } from 'aws-sdk-mock';` or CommonJS `const { mock, restore, remock } = require('aws-sdk-mock');`","message":"The README examples often show `AWS.mock(...)` for usage, which can be misleading. The library exports `mock`, `restore`, and `remock` as named exports.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Install the AWS SDK v2: `npm install aws-sdk@2` or `yarn add aws-sdk@2`.","cause":"The 'aws-sdk' package is not installed in your project, or is not resolvable by Node.js.","error":"Error: Cannot find module 'aws-sdk'"},{"fix":"Use a named import: `import { mock, restore } from 'aws-sdk-mock';`","cause":"You are attempting to import the `mock` function incorrectly, likely as a default import when it is a named export.","error":"TypeError: (0, aws_sdk_mock_1.mock) is not a function"},{"fix":"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.","cause":"This error often occurs when trying to mock an AWS service or method that either doesn't exist on the `aws-sdk` object, or the `aws-sdk` client itself hasn't been properly configured/initialized in a way that `aws-sdk-mock` can intercept it. This is common when attempting to use it with AWS SDK v3 clients.","error":"TypeError: Cannot read properties of undefined (reading 'putObject') (or similar for other AWS services/methods)"}],"ecosystem":"npm"}