npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-aws-simple-mock

v0.12.4

Published

This library provides simple mock methods for aws-sdk in Jest testing.

Downloads

611

Readme

jest-aws-simple-mock

This library provides simple mock methods for aws-sdk in Jest testing.

SDK references are here.

AWS SDK for Javascript Version 2
AWS SDK for Javascript v3
DynamoDB Data Mapper

Latest version is 0.12.4
npm version .
See npm.
This library is compatible with AWS-SDK v3 version v3.517.0.

[Important Note]

Version 0.8.0 has a breaking changes from previous version.

Breaking changes

  • [AWS-SDK-V3] mockSesV2 was deprecated. use mockSESv2 instead.
  • [AWS-SDK-V2] mockAcm was deprecated. use mockACM instead.
  • [AWS-SDK-V2] mockSes was deprecated. use mockSES instead.
  • [AWS-SDK-V2] mockSesV2 was deprecated. use mockSESV2 instead.
  • [AWS-SDK-V2] mockEcs was deprecated. use mockECS instead.
  • [AWS-SDK-V2] mockSsm was deprecated. use mockSSM instead.
  • [AWS-SDK-V2] mockKms was deprecated. use mockKMS instead.

Supported AWS-SDK

  • @aws/dynamodb-datamapper
  • aws-sdk(version 2)
  • @aws-sdk(version 3)

install

yarn

yarn add jest-aws-simple-mock --dev

npm

npm install jest-aws-simple-mock --save-dev

Exported modules

export * from './mocks_v2' // For AWS-SDK version 2
export * as V3 from './mocks_v3' // For AWS-SDK version 3
export * from './chain' // Mock chaining Utility

Basics

  1. All modules have been named as "mock + [AWS SDK Client Name]"
    For example, if you want to mock ACM module, mock name is "mockACM".

  2. All methods has 3 types of mock behavior("OneShot", "Entire", "Rejection")

    • OneShot type mocks only the call.
    • Entire type mocks all calls.
    • Rejection type mocks as Promise.reject.

For example, If you want to mock S3.putObject, You can use 3 following mocks.

  // This is OneShot mock method. Would be mocked only at the call.
  putObject: (result:any, mock?: jest.SpyInstance): jest.SpyInstance => {
    return attachMock('putObject', 'S3', Promise.resolve(result), true, mock)
  },
  // This is Entire mock method. Would be mocked all calls.
  putObjectAll: (result:any, mock?: jest.SpyInstance): jest.SpyInstance => {
    return attachMock('putObject', 'S3', Promise.resolve(result), false, mock)
  },

  // This is Rejection mock method. Promise.reject would be rose. 
  putObjectThrow: (result:any, mock?: jest.SpyInstance): jest.SpyInstance => {
    return attachMock('putObject', 'S3', Promise.reject(result), true, mock)
  },

Usage

All modules are along with AWS-SDK module name except DynamoDB Data Mapper.

AWS-SDK version 3 modular style

import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3'
import { V3 } from 'jest-aws-simple-mock'

async function mockS3PutObject () {
  const spy = V3.mockS3.send({}) // Mocking response of putObject as {}

  const client = new S3Client()
  const command = new PutObjectCommand(...)
  const res = await client.send() // The res would be return {}
}

AWS-SDK version 3 previous version compatible style

import { S3 } from '@aws-sdk/client-s3'
import { V3 } from 'jest-aws-simple-mock'

async function mockS3PutObject () {
  const spy = V3.mockS3.putObject({}) // Mocking response of putObject as {}

  const s3 = new S3({region: '.....'})
  const res = await s3.putObject() // The res would be return {}
}

AWS-SDK version 2

// AWS-SDK version 2
import { S3 } from 'aws-sdk'
import { mockS3 } from 'jest-aws-simple-mock'

async function mockS3PutObject () {
  const spy = mockS3.putObject({}) // Mocking response of putObject as {}

  const s3 = new S3()
  const res = await s3.putObject(...).promise() // The res would be return the {}
}

DynamoDB DataMapper

use mockDynamo module

import { mockDynamo } from 'jest-aws-simple-mock'
import { DataMapper } from '@aws/dynamodb-data-mapper'
const dataMapper = new DataMapper({Client: new DynamoDB()})

// Mock DataMapper.query method
mockDynamo.query([{a}, {b}])

// Run DataMapper method
const q = dataMapper.query(...)

let res = []
for await (const data of q) res.push(data)

// Result: would be the one that have been mocked. ==> [{a}, {b}]
return res

Chaining Utility

For complex mocking, You can use mock chaining object.

Let you make concern these example.

Sample.ts

function a () {
    const res1 = Lambda.invoke(...)
    :
    const res2 = DynamoDB.getItem(...)
    :
    const res3 = DynamoDB.updateItem(...)
    :
    const res4 = Lambda.invoke(...)
}

SampleMock.ts

import { MockChain } from 'jest-aws-simple-mock'

const chain = new MockChain()
chain
  .append('spyLambdaInvoke', mockLambda.invoke, {}) // (1): mock for 1st lambda invoke
  .append('spyDynamoGet', mockDynamoDB.getItem, {}) // (2): mock for dynamodb getItem
  .append('spyDynamoUpdate', mockDynamoDB.updateItem, {}) // (3): mock for dynamodb updateItem
  .append('spyLambdaInvoke', mockLambda.invoke, {}) // (4): mock for 2nd lambda invoke (by specify the same name of the (1))
  
const spy = chain.spies.spyLambdaInvoke
expect(spy).toHaveBeenCalled()

Development

All mocks have been generated from mock generator.
If you'd like to make some changes to mocks, Consider to change generator.
The generator also has been included in the generator folder.