jest-aws-sdk-mock
Create Jest Mocks for AWS SDK services.
This module was created to help test AWS Lambda functions but can be used in any situation where the AWS SDK needs to be mocked. This is a rewrite of https://github.com/dwyl/aws-sdk-mock but using jest under the hood instead of sinon.js.
What?
Uses Jest under the hood to mock the AWS SDK services and their associated methods.
How? (Usage)
jest-aws-sdk-mock
from NPM
install npm install jest-aws-sdk-mock --save-dev
Use in your Tests
Using plain JavaScript
const AWSMock = ; AWSMock; AWSMock; // S3 getObject mock - return a Buffer object with file dataAWSMock; /** TESTS**/ AWSMock;AWSMock;AWSMock;// or AWSMock.restore(); this will restore all the methods and services
Using TypeScript
;;; describe'the module',;
Notes:
- The AWS Service needs to be initialised after the invocation of the
mock
function
The below won't work
;; ;// won't override sns.publishAWSMock.mock'SNS', 'publish', 'message';
- The mock function will be overwritten if you call mock on the same method again
AWSMock.mock'SNS', 'publish', ;;AWSMock.mock'SNS', 'publish', ;sns.publish as any, ;
- Individual SDK import will work
AWSMock.mock'S3', 'getObject', ;;;;expectresult.toEqual'message';done;
Nested services
It is possible to mock nested services like DynamoDB.DocumentClient
. Simply use this dot-notation name as the service
parameter to the mock()
and restore()
methods:
AWS;
NB: Use caution when mocking both a nested service and its parent service. The nested service should be mocked before and restored after its parent:
// OKAWS;AWS;AWS;AWS; // Not OKAWS;AWS; // Not OKAWS;AWS;
aws-sdk
module explicitly
Setting the Project structures that don't include the aws-sdk
at the top level node_modules
project folder will not be properly mocked. An example of this would be installing the aws-sdk
in a nested project directory. You can get around this by explicitly setting the path to a nested aws-sdk
module using setSDK()
.
Example:
const path = ;const AWS = ; AWS; /** TESTS**/
aws-sdk
object explicitly
Setting the Due to transpiling, code written in TypeScript or ES6 may not correctly mock because the aws-sdk
object created within aws-sdk-mock
will not be equal to the object created within the code to test. In addition, it is sometimes convenient to have multiple SDK instances in a test. For either scenario, it is possible to pass in the SDK object directly using setSDKInstance()
.
Example:
// test codeconst AWSMock = ;;AWSMock;AWSMock; // implementation codeconst sqs = ;
Configuring promises
If your environment lacks a global Promise constructor (e.g. nodejs 0.10), you can explicitly set the promises on aws-sdk-mock
. Set the value of AWS.Promise
to the constructor for your chosen promise library.
Example (if Q is your promise library of choice):
const AWS = Q = ; AWSPromise = QPromise; /** TESTS**/
Documentation
AWS.mock(service, method, replace)
Replaces a method on an AWS service with a replacement function or string.
Param | Type | Optional/Required | Description |
---|---|---|---|
service |
string | Required | AWS service to mock e.g. SNS, DynamoDB, S3 |
method |
string | Required | method on AWS service to mock e.g. 'publish' (for SNS), 'putItem' for 'DynamoDB' |
replace |
string or function | Required | A string or function to replace the method |
AWS.restore(service, method)
Removes the mock to restore the specified AWS service
Param | Type | Optional/Required | Description |
---|---|---|---|
service |
string | Optional | AWS service to restore - If only the service is specified, all the methods are restored |
method |
string | Optional | Method on AWS service to restore |
If AWS.restore
is called without arguments (AWS.restore()
) then all the services and their associated methods are restored
i.e. equivalent to a 'restore all' function.
AWS.setSDK(path)
Explicitly set the require path for the aws-sdk
Param | Type | Optional/Required | Description |
---|---|---|---|
path |
string | Required | Path to a nested AWS SDK node module |
AWS.setSDKInstance(sdk)
Explicitly set the aws-sdk
instance to use
Param | Type | Optional/Required | Description |
---|---|---|---|
sdk |
object | Required | The AWS SDK object |