-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for mocking AWS SDK's promises #24
Conversation
As of March 31, 2016 [the AWS SDK now supports Promises](https://blogs.aws.amazon.com/javascript/post/Tx3BZ2DC4XARUGG/Support-for-Promises-in-the-SDK). This means that you can now do things like this: ```js var s3 = new AWS.S3({apiVersion: '2006-03-01', region: 'us-west-2'}); var params = { Bucket: 'bucket', Key: 'example2.txt', Body: 'Uploaded text using the promise-based method!' }; var putObjectPromise = s3.putObject(params).promise(); putObjectPromise.then(function(data) { console.log('Success'); }).catch(function(err) { console.log(err); }); ``` Attempting to use aws-sdk-mock to mock methods that now no longer require a callback function causes issues, most notably: `TypeError: callback is not a function`. This PR attempts to resolve this issue by checking to see if the mocked method was passed a callback function. If it was passed a callback function then everything should behave exactly as it did before. But if it wasn't then it returns an object containing a single method `promise` which, when invoked, will execute the user's mocked response and the promise will branch based on what the callback returns. It will utilize the native/global promise if available, but it will also pick up on calls to `AWS.config.setPromisesDependency` so that the mocks can create new promises with the provided promise constructor.
@jcready thanks for the PR! would you mind adding some tests & docs (in the readme) for this addition? |
Current coverage is 100%@@ master #24 diff @@
===================================
Files 1 1
Lines 58 73 +15
Methods 0 0
Messages 0 0
Branches 0 0
===================================
+ Hits 58 73 +15
Misses 0 0
Partials 0 0
|
@nelsonic to properly test this I would either need to depend on a native Promise implementation (which Node 0.10.36 doesn't have AFAIK) or I would have to include a polyfill for it as a devDependency. What's your preference? |
Good question. I think we should probably migrate to using |
I've added tests for environments that support Promises natively. I'm explicitly telling Istanbul to ignore one line because it would require that you test against an older version of the aws-sdk, which that line is specifically trying to handle. |
@nikhilaravi looks fine to me |
@nikhilaravi yeah, looks good. |
@nelsonic done. |
@jcready ah looks like there are some conflicts - would you be able to resolve them? |
@jcready thanks for updating the version, please let us know when merge conflicts are resolved so we can merge and |
# Conflicts: # package.json
@nikhilaravi @nelsonic I've resolve the merge conflict (literally just the package.json version). |
@jcready let's ship it! |
As of March 31, 2016 the AWS SDK now supports Promises. This means that you can now do things like this:
Attempting to use aws-sdk-mock to mock methods that now no longer require a callback function causes issues, most notably:
TypeError: callback is not a function
. This PR attempts to resolve this issue by checking to see if the mocked method was passed a callback function. If it was passed a callback function then everything should behave exactly as it did before. But if it wasn't then it returns an object containing a single methodpromise
which, when invoked, will execute the user's mocked response and the promise will branch based on what the callback returns. It will utilize the native/global promise if available, but it will also pick up on calls toAWS.config.setPromisesDependency
so that the mocks can create new promises with the provided promise constructor.