Skip to content

Instantly share code, notes, and snippets.

@joerx

joerx/index.js

Last active Sep 15, 2020
Embed
What would you like to do?
Mocking S3 in Node.js using Sinon
var Aws = require('aws-sdk');
var sinon = require('sinon');
// Only works for 'createBucket', 'update' and a few others since most API methods are generated dynamically
// upon instantiation. Very counterintuitive, thanks Amazon!
var createBucket = sinon.stub(Aws.S3.prototype, 'createBucket');
createBucket.yields(null, 'Me create bucket');
// For other methods, we can 'assign' the stubs to the proto, already defined function won't be overridden
var listBuckets = Aws.S3.prototype.listBuckets = sinon.stub();
listBuckets.yields(null, 'Me list buckets');
var s3 = new Aws.S3();
s3.createBucket({Bucket: 'my-bucket'}, function(err, arg1) {
console.log(arg1);
});
s3.listBuckets(function(err, arg1) {
console.log(arg1);
});
console.log('Called #createBucket() ' + s3.createBucket.callCount + ' time(s)');
console.log('Called #listBuckets() ' + s3.listBuckets.callCount + ' time(s)');
{
"name": "node-s3-stub",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "BSD",
"dependencies": {
"sinon": "~1.14.1",
"aws-sdk": "~2.1.17"
}
}
@sidhantpanda

This comment has been minimized.

Copy link

@sidhantpanda sidhantpanda commented Aug 28, 2017

Thank you for putting this out there!

@sidhantpanda

This comment has been minimized.

Copy link

@sidhantpanda sidhantpanda commented Aug 28, 2017

For putObject:

const putObjectStub = AWS.S3.prototype.putObject = sinon.stub();
putObjectStub.yields(null, 'data');
@joaoreynolds

This comment has been minimized.

Copy link

@joaoreynolds joaoreynolds commented Sep 2, 2017

You guys are lifesavers. All of the sudden aws-sdk-mock stopped mocking and I couldn't trace the cause (plus, deps on that library are outdated and there have been no commits in about half a year).

I should have just been doing this all along.

@akazakou

This comment has been minimized.

Copy link

@akazakou akazakou commented Nov 9, 2017

@joerx thank you

@nguyeti

This comment has been minimized.

Copy link

@nguyeti nguyeti commented Dec 3, 2017

Thanks for your help.

does it work with getObjects and listObjects?

Thanks @joerx

@AnjanaMysore

This comment has been minimized.

Copy link

@AnjanaMysore AnjanaMysore commented Feb 20, 2019

Thanks, this helped me to write my unit tests

@aruna-h

This comment has been minimized.

Copy link

@aruna-h aruna-h commented Apr 11, 2019

hello how can i write testcase for uploading to s3, and create bucket in nodejs

@sisplatform-support

This comment has been minimized.

Copy link

@sisplatform-support sisplatform-support commented May 31, 2019

Nice. Great example. It works me for listObjectsV2 also. I think it will support all.

@dineshrawat11

This comment has been minimized.

Copy link

@dineshrawat11 dineshrawat11 commented Jul 8, 2019

Nice. Great example. It works me for listObjectsV2 also. I think it will support all.

Hi, Can you please share you code for mocking listObjectsV2?

@jeffstringer

This comment has been minimized.

Copy link

@jeffstringer jeffstringer commented Oct 23, 2019

Thank you @joerx 😄

@bluepioupiou

This comment has been minimized.

Copy link

@bluepioupiou bluepioupiou commented Nov 28, 2019

thanks a lot

@prakharvk

This comment has been minimized.

Copy link

@prakharvk prakharvk commented Dec 30, 2019

how to create such stub for s3.upload and how to restore them ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.