Skip to content

Instantly share code, notes, and snippets.

@jacoor
Last active April 22, 2020 12:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jacoor/e6cea15112cbc41b6885172c460037d7 to your computer and use it in GitHub Desktop.
Save jacoor/e6cea15112cbc41b6885172c460037d7 to your computer and use it in GitHub Desktop.
Manual mocks for aws-sdk.js using jest.
// inspired by: https://derikwhittaker.blog/2018/02/20/using-manual-mocks-to-test-the-aws-sdk-with-jest/
let AWS = {
// This here is to allow/prevent runtime errors if you are using
// AWS.config to do some runtime configuration of the library.
// If you do not need any runtime configuration you can omit this.
config: {
setPromisesDependency: (arg) => {},
update: (arg) => {},
},
Kinesis: function() {
},
STS: function() {
},
Lambda: function() {
},
DynamoDB: {
DocumentClient: function (params) {
}
},
CloudWatch: function() {
},
VERSION: "2.264.1",
};
// Because I care about using the S3 service's which are part of the SDK
// I need to setup the correct identifier.
//
AWS.Kinesis.prototype = {
...AWS.Kinesis.prototype,
// Stub for the putRecord method in the sdk
putRecord(params, callback) {
callback(null);
},
createStream(params, callback) {
// console.log("create stream mock");
callback(null);
},
deleteStream(params, callback) {
// console.log("delete stream mock");
callback(null);
},
waitFor(state, params, callback) {
// console.log("wait for mock");
callback(null);
},
describeStream(params, callback) {
// console.log("describeStream mock");
callback(null, {
"StreamDescription": {
"KeyId": null,
"EncryptionType": "NONE",
"StreamStatus": "ACTIVE",
"StreamName": "process_order_mssql_local",
"Shards": [{
"ShardId": "shardId-000000000000",
"HashKeyRange": {
"EndingHashKey": "340282366920938463463374607431768211455",
"StartingHashKey": "0"
},
"SequenceNumberRange": {
"StartingSequenceNumber": "49585023131013678200177316516963950164506062627784359938"
}
}],
"StreamARN": "arn:aws:kinesis:us-east-1:xxxxxxxxxxx:stream/xxxxxx",
"EnhancedMonitoring": [{
"ShardLevelMetrics": []
}],
"StreamCreationTimestamp": 1527880065.0,
"RetentionPeriodHours": 24
}
});
}
};
AWS.Lambda.prototype = {
...AWS.Lambda.prototype,
createEventSourceMapping(params, callback) {
// console.log("createEventSourceMapping mock");
callback(null, {"UUID": "exampleFakeUUID"});
},
updateEventSourceMapping(params, callback) {
callback(null);
},
deleteEventSourceMapping(params, callback) {
callback(null);
}
};
AWS.DynamoDB.DocumentClient.prototype = {
...AWS.DynamoDB.DocumentClient.prototype,
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
*/
if (params.Key.merchant_id === 800) {
callback(null, {"Item": {"is_active": true}});
} else {
callback(null); // FIXME: is this proper? what does kinesis return for get if nothing found?
}
}
};
AWS.CloudWatch.prototype = {
...AWS.CloudWatch.prototype,
putMetricAlarm(params = {}, callback) {
callback(null);
},
deleteAlarms(params = {}, callback) {
callback(null);
},
};
// Export my AWS function so it can be referenced via requires
module.exports = AWS;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment