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

@mindhive/mock-aws

v2.1.0

Published

Mocking of the aws-sdk

Downloads

17

Readme

Mock AWS

Mocking AWS API calls using Sinon

Use either mockService to mock an entire service, or mockMethod to mock just a single method. Both take the AWS service class (e.g. what you get from import S3 from 'aws-sdk/clients/s3') and the method names/name you want mocked. Optionally you can set the specific API version you want, it will default to the latest.

The resulting methods are sinon.spys. You can use them in the usual way in your spec/test assertions. The method also has a property request. This is a sinon.stub. Use it as usual to specify the return value. If you want your test to be asynchronous and pause on the method call then use a Promise as the return value and resolve it later.

The methods will validate the passed parameters against the 'schema' AWS has catching errors early.

import S3 from 'aws-sdk/clients/s3'
import { mockService } from '@mindhive/mock-aws'

const s3 = mockService(S3, ['getObject', 'putObject'])
const image = some.image()
s3.getObject.request.withArgs(sinon.match({ Key: expectedKey }))
  .returnsValue({ Body: image })
const result = await s3.getObject(params).promise()
s3.getObject.should.have.been.calledOnce
result.Body.should.equal(image)

Testing against DynamoDB

This package installs a bin script dynamodb-local-install which will download AWS's DynamoDB Local. This is stored within node_modules so you will need to reinstall should the package be updated. Then you can start a copy of the local server and get a DynamoDB client pointing to that server with dynamodbClientInstance. The instance is created on a free port so you can run tests in parallel across multiple instances of Node.

Note: the local server requires Java to be installed.

givenTableCreated will ensure the table is setup as per the given properties and empty. As createTable and deleteTable can be slow with the local server it will empty the table in a batch operation if the table properties have not changed.

Example:

import dynamodbClientInstance from '@mindhive/mock-aws/dynamodbClientInstance'
import givenTableCreated from '@mindhive/mock-aws/givenTableCreated'
import DynamoDb from 'aws-sdk/clients/dynamodb'

let dynamodb
before(async () => {  // This can be put in a separate module imported by all tests
  // Ensure local server is started before the test runs
  dynamodb = await dynamodbClientInstance()
})

describe('suite', () => {
  it('should work with DynamoDB table', async () => {
    await givenTableCreated({
      TableName: 'foo',
      ...  // As per DynamoDB.createTable in the AWS SDK
    })
    const docClient = new DynamoDb.DocumentClient({ service: dynamodb })
    await docClient.put({...}).promise()
    ...
  })
})

Table definition from CloudFormation template

You can load the DynamoDB table definition directly from your CloudFomration template ensuring your tests match your production setup.

import givenTableCreated from '@mindhive/mock-aws/givenTableCreated'
import loadCfnYaml from '@mindhive/mock-aws/loadCfnYaml'

const template = loadCfnYaml('resources.cfn.yaml')
await givenTableCreated(template.Resources['FooTable'].Properties)

This will also handle some of the differences between AWS::DynamoDB::Table Properties and the properties required by the SDK to create a table.

Single DynamoDB Local instance

Alternatively you can call the bin script dynamodb-local which downloads the server (if not downloaded already) and starts the local server on the (fixed) standard port. dynamodbClientInstance will look on that port first and use it if it exists.