Skip to content

Instantly share code, notes, and snippets.

@joerx
Last active May 17, 2023 12:58
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save joerx/9ef0cb5b2c4252f3d5ff to your computer and use it in GitHub Desktop.
Save joerx/9ef0cb5b2c4252f3d5ff to your computer and use it in GitHub Desktop.
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
Copy link

Thank you for putting this out there!

@sidhantpanda
Copy link

sidhantpanda commented Aug 28, 2017

For putObject:

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

@joaoreynolds
Copy link

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
Copy link

akazakou commented Nov 9, 2017

@joerx thank you

@nguyeti
Copy link

nguyeti commented Dec 3, 2017

Thanks for your help.

does it work with getObjects and listObjects?

Thanks @joerx

@AnjanaMysore
Copy link

Thanks, this helped me to write my unit tests

@aruna-h
Copy link

aruna-h commented Apr 11, 2019

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

@sisplatform-support
Copy link

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

@dineshrawat11
Copy link

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
Copy link

Thank you @joerx 😄

@bluepioupiou
Copy link

thanks a lot

@prakharvk
Copy link

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

@Shinobi247
Copy link

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

const uplaodStub = (AWS.S3.prototype.upload= sinon.stub());
uplaod.yields(null, 'data');

@piyushjaware
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment