Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

aws-sdk-mock

Package Overview
Dependencies
Maintainers
5
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aws-sdk-mock

Functions to mock the JavaScript aws-sdk

  • 6.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
5
Created

What is aws-sdk-mock?

The aws-sdk-mock npm package is a tool for mocking AWS SDK methods for unit testing purposes. It allows developers to simulate AWS service responses and behaviors without making actual calls to AWS services, which can save time and cost during development and testing.

What are aws-sdk-mock's main functionalities?

Mocking AWS SDK Methods

This feature allows you to mock specific methods of AWS services. In this example, the `getObject` method of the S3 service is mocked to return 'mocked data' instead of making an actual call to AWS.

const AWS = require('aws-sdk');
const AWSMock = require('aws-sdk-mock');

AWSMock.mock('S3', 'getObject', (params, callback) => {
  callback(null, { Body: 'mocked data' });
});

const s3 = new AWS.S3();
s3.getObject({ Bucket: 'example-bucket', Key: 'example-key' }, (err, data) => {
  if (err) console.log(err);
  else console.log(data.Body.toString()); // Output: mocked data
});

AWSMock.restore('S3');

Restoring Mocked Methods

This feature allows you to restore the original AWS SDK methods after they have been mocked. In this example, the `putItem` method of the DynamoDB service is mocked and then restored to its original state.

const AWS = require('aws-sdk');
const AWSMock = require('aws-sdk-mock');

AWSMock.mock('DynamoDB', 'putItem', (params, callback) => {
  callback(null, { Attributes: { id: { S: '123' } } });
});

const dynamoDB = new AWS.DynamoDB();
dynamoDB.putItem({ TableName: 'example-table', Item: { id: { S: '123' } } }, (err, data) => {
  if (err) console.log(err);
  else console.log(data.Attributes); // Output: { id: { S: '123' } }
});

AWSMock.restore('DynamoDB');

Mocking Multiple Methods

This feature allows you to mock multiple methods of the same AWS service. In this example, both the `putObject` and `deleteObject` methods of the S3 service are mocked.

const AWS = require('aws-sdk');
const AWSMock = require('aws-sdk-mock');

AWSMock.mock('S3', 'putObject', (params, callback) => {
  callback(null, { ETag: '"mocked-etag"' });
});
AWSMock.mock('S3', 'deleteObject', (params, callback) => {
  callback(null, { DeleteMarker: true });
});

const s3 = new AWS.S3();
s3.putObject({ Bucket: 'example-bucket', Key: 'example-key', Body: 'data' }, (err, data) => {
  if (err) console.log(err);
  else console.log(data.ETag); // Output: "mocked-etag"
});
s3.deleteObject({ Bucket: 'example-bucket', Key: 'example-key' }, (err, data) => {
  if (err) console.log(err);
  else console.log(data.DeleteMarker); // Output: true
});

AWSMock.restore('S3');

Other packages similar to aws-sdk-mock

Keywords

FAQs

Package last updated on 11 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc