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

aws-sdk-client-mock

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aws-sdk-client-mock

Easy and powerful mocking of AWS SDK v3 Clients

  • 4.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
994K
increased by7.03%
Maintainers
1
Weekly downloads
 
Created

What is aws-sdk-client-mock?

The aws-sdk-client-mock package is a mocking library for AWS SDK v3 clients. It allows developers to mock AWS SDK v3 client methods for unit testing purposes, making it easier to test code that interacts with AWS services without making actual calls to AWS.

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

Mocking AWS SDK v3 Client Methods

This feature allows you to mock methods of AWS SDK v3 clients. In the example, the S3Client's GetObjectCommand is mocked to return 'mocked data' instead of making an actual call to AWS S3.

const { mockClient } = require('aws-sdk-client-mock');
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');

const s3Mock = mockClient(S3Client);
s3Mock.on(GetObjectCommand).resolves({ Body: 'mocked data' });

async function getObject() {
  const client = new S3Client();
  const command = new GetObjectCommand({ Bucket: 'example-bucket', Key: 'example-key' });
  const response = await client.send(command);
  return response.Body;
}

getObject().then(data => console.log(data));

Resetting Mocks

This feature allows you to reset the mocks, which is useful for cleaning up between tests. In the example, the mock for GetObjectCommand is reset, so any subsequent calls will not use the mock.

const { mockClient } = require('aws-sdk-client-mock');
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');

const s3Mock = mockClient(S3Client);
s3Mock.on(GetObjectCommand).resolves({ Body: 'mocked data' });

// Reset the mock
s3Mock.reset();

async function getObject() {
  const client = new S3Client();
  const command = new GetObjectCommand({ Bucket: 'example-bucket', Key: 'example-key' });
  try {
    const response = await client.send(command);
    return response.Body;
  } catch (error) {
    console.error('Error:', error);
  }
}

getObject();

Mocking Multiple Commands

This feature allows you to mock multiple commands for a single AWS SDK v3 client. In the example, both GetObjectCommand and PutObjectCommand are mocked with different responses.

const { mockClient } = require('aws-sdk-client-mock');
const { S3Client, GetObjectCommand, PutObjectCommand } = require('@aws-sdk/client-s3');

const s3Mock = mockClient(S3Client);
s3Mock.on(GetObjectCommand).resolves({ Body: 'mocked get data' });
s3Mock.on(PutObjectCommand).resolves({ ETag: 'mocked-etag' });

async function performS3Operations() {
  const client = new S3Client();
  const getObjectCommand = new GetObjectCommand({ Bucket: 'example-bucket', Key: 'example-key' });
  const putObjectCommand = new PutObjectCommand({ Bucket: 'example-bucket', Key: 'example-key', Body: 'data' });
  const getObjectResponse = await client.send(getObjectCommand);
  const putObjectResponse = await client.send(putObjectCommand);
  return { getObjectResponse, putObjectResponse };
}

performS3Operations().then(data => console.log(data));

Other packages similar to aws-sdk-client-mock

Keywords

FAQs

Package last updated on 23 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