Skip to content
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

How do we mock s3.getObject(...).promise? #1973

Closed
manjufy opened this issue Mar 15, 2018 · 3 comments
Closed

How do we mock s3.getObject(...).promise? #1973

manjufy opened this issue Mar 15, 2018 · 3 comments
Labels
guidance Question that needs advice or information.

Comments

@manjufy
Copy link

manjufy commented Mar 15, 2018

When I try to mock, s3.getObject(...).promise, I get the following error.

TypeError: s3.getObject(...).promise is not a function
@AllanZhengYP
Copy link
Contributor

Hi @manju16832003
s3.getObject and almost all other operations are initialized and bound to AWS object at runtime, so you cannot find the function. They are just not there yet. But it's available after you instantiate the s3 client.

const s3 = new AWS.S3({region: 'region'});
console.log('promise is a function: ', s3.getObject(params).promise);

I don't have the information of where this error is raised. But I write a simplified mock function that should work:

var AWS = require('aws-sdk');
describe ('mock promise', () => {
    it ('test', () => {
        var s3 = new AWS.S3({region: 'us-west-2'});
        var request = s3.putObject({
            Bucket: 'bucket', 
            Key: 'key'
        });
        request.promise = () => Promise.resolve('mockdata');
        request.promise().then((data) => {expect(data).toEqual('mockResp')})
    })
})

Here the request is sent on wire still.

@manjufy
Copy link
Author

manjufy commented Mar 22, 2018

@AllanFly120 That was helpful thanks a lot 👍

@manjufy manjufy closed this as completed Mar 22, 2018
@srchase srchase added guidance Question that needs advice or information. and removed Question labels Jan 4, 2019
@lock
Copy link

lock bot commented Sep 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

3 participants