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

aws-service-mock-sinon

v1.1.0

Published

A Sinon-based AWS Service mocking module for NodeJS and ExpressJS

Downloads

9

Readme

NPM

npm versionBuild StatusCoverage Status

awsServiceMock

A quick and simple library that lets you use Sinon stubs with aws-sdk.

Why is this needed?

aws-sdk creates services in a weird way, so it isn't possible to do, say:

sinon.stub(AWS.S3.prototype, "getObject").returns({
   an: "object"
})

Because AWS.S3.prototype doesn't actually have a function called getObject. It is possible to just stub an instance of new AWS.S3, but chances are you instantiating that in your non-test code, and don't want to structure it weirdly just so that you can run tests properly.

How do I use it?

Create Mock - createAwsMock(service, method, function)

Params

service: String - name of AWS service to be mocked method: String - name of AWS service method to be mocked function: Function() - callback function to replace AWS.service.method() call

Details

Then, rather than call sinon.stub, you can call this module as a function, which will return a stub. Like so:

const { createAwsMock } = require('awsServiceMock');

createAwsMock('S3', 'getObject').returns({
    an: 'object',
});

new AWS.S3().getObject({Bucket: 'test'}, function(err, response) {
    assert.equal(response.an, 'object'),
});

Get Mock - getAwsMock(service, method)

Params

service: String - name of AWS service to be mocked method: String - name of AWS service method to be mocked

Returns

awsMock: Object - Sinon Stub

Details

If you wish to use the Sinon verification helpers, you can get run the function again to retrieve the same stub. So instead of doing:

AWS.S3.prototype.getObject.calledOnce();

you write:

const { getAwsMock } = require('awsServiceMock');

getAwsMock('S3','getObject').calledOnce();

Update Mock - updateAwsMock(service, method, function)

Params

service: String - name of AWS service to be mocked method: String - name of AWS service method to be mocked function: Function() - callback function to replace AWS.service.method() call

Details

If you need to replace a mock, you can now call updateAwsMock() instead of calling deleteAwsMock() followed by createAwsMock():

const { updateAwsMock } = require('awsServiceMock');

updateAwsMock('S3','getObject').returns({
    an: 'idea',
});

new AWS.S3().getObject({Bucket: 'test'}, function(err, response) {
    assert.equal(response.an, 'idea'),
});

Delete Mock - deleteAwsMock(service, method)

Params

service: String - name of AWS service mock to be deleted method: String - name of AWS service method mock to be deleted

Details

If you need to change the callback function for a mock, you must first delete the previous mock via the following:

const { deleteAwsMock } = require('awsServiceMock');

deleteAwsMock('S3','getObject');

It is recommended to call this in either the afterEach() method or at the end of each of your test cases as you may experience inconsistent behavior otherwise.

Updated v1.1.0: You can now chose to called updateAwsMock() at the beginning of each test where you need to change the function callback. Both the afterEach() and updateAwsMock() approaches should solve any issues with mock collisions.

Clean Up

To restore the standard AWS Send functionally, you should call the following:

const { restoreAwsRequestSend } = require('awsServiceMock');

restoreAwsRequestSend();

How does it actually work?

It stubs out AWS.Request.send, which is available. That stub then returns a mock AWS.Response object with the return value you have provided. This idea was copied from fakeaws, which works great except that I couldn't find a way to call verification methods on stubbed code, which this allows you to do.

Credits

This was originally forked from mock-aws-sinon, but was updated to use the latest dependency versions and functionally overhauled to add new features (breaking compared to the original source).

Liability

This is a personal project I maintain on my own time. I will continue to extend and improve this as I am able. As such, this module is for use as is, with no guarantees or liability. Feel free to pursue the test cases for validation and please let me know if you have any feedback or find any bugs!