Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const stubs = require('./aws-stubs');
const AWS = {};
AWS.config = {
setPromisesDependency: (arg) => {}
};
AWS.S3 = function() {
}
AWS.S3.prototype = {
...AWS.S3.prototype,
// Stub for the listObjectsV2 method in the sdk
listObjectsV2(params){
const stubPromise = new Promise((resolve, reject) => {
resolve(stubs.listObjects);
});
return {
promise: () => {
return stubPromise;
}
}
}
};
module.exports = AWS;
@dnafication

This comment has been minimized.

Copy link

@dnafication dnafication commented Aug 28, 2020

Hi Derik, I tried following the same thing as per your blog post with following as my aws-sdk.ts in the __mock__ dir

const AWS = {
  config: {
    setPromisesDependency: (arg) => {},
    update: (arg) => {},
  },
  Lambda: function() {},
  DynamoDB: {
    DocumentClient: function(params) {
      console.log('DocumentClient is instantiated');
    },
  },
  CloudWatch: function() {},
};

AWS.DynamoDB.DocumentClient.prototype = {
  ...AWS.DynamoDB.DocumentClient.prototype,
  put(params, callback) {
    console.log('Dynamo DocumentClient put mock', params);
    callback(null, {});
  },
  update(params, callback) {
    // console.log("Dynamo DocumentClient updateItem mock")
    callback(null, {});
  },
  get(params, callback) {
    // console.log("Dynamo DocumentClient get mock")
    /*
       https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#get-property
      */
    callback(null, {});
  },
};

export default AWS;

however I get following error on the file where aws sdk is being imported.

Test suite failed to run

    TypeError: Cannot read property 'inherit' of undefined

    > 1 | import * as AWS from 'aws-sdk';
        | ^
      2 | 

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