mock-aws
Easily mock aws-sdk API methods to enable easier testing of applications which use the AWS SDK for JavaScript.
Under the hood, this stubs aws-sdk methods using sinon.js.
API
AWS.mock(service, method, data)
Mocks an AWS service method to return the specified test data or a function
Arguments
- service (string): the name of the AWS service that the method belongs to e.g. EC2, Route53 etc.
- method (string): the service's method to be be mocked e.g. describeTags
- data (object): the test data that the mocked method should return or a function that is called
The parameter data
can be either fixed data, in which case the original callback will be called with that data, or it can be a function. If it is a function, then when the mocked service is called, your function will be called.
If it is a function, it will be passed all of the parameters passed to the original AWS SDK call, including the callback. You are expected to call the callback.
Example of fixed data:
var AWS = ;var ec2 = ; AWS;ec2;
Example of function:
var AWS = ;var ec2 = ; AWS;ec2;ec2;ec2;
AWS.restore(service, method)
Removes mocked service method to restore the original functionality
Arguments
- service (string): the name of the AWS service that the method belongs to e.g. EC2, Route53 etc.
- method (string): the service's method to be be restored e.g. describeTags
var AWS = ; // Mock some EC2 methods to return specified test dataAWS;var ec2 = ; // EC2 methods should return specified test dataec2; // Call restore with a service & methodAWS; // describeTags method now returns real AWS dataec2;
AWS.restore(service)
Removes ALL mocked methods for given service to restore the original functionality
Arguments
- service: (string) the name of the AWS service to restore e.g. EC2, Route53 etc.
var AWS = ;var ec2 = ; AWS;AWS;ec2;ec2; AWS;ec2;ec2;
AWS.restore()
Removes ALL mocked services & methods to restore the original functionality
var AWS = ; var ec2 = ;var s3 = ; AWS;AWS; AWS; ec2;ec2; s3 AWS;ec2;ec2;s3
Example
I want to build and test a tag validator that returns the id's of any EBS volumes which don't have a tag called 'name'. My production code for my tag-validator may look something like the following:
var AWS = ; { var ec2 = region: region ; ec2; } moduleexports = getNamelessVolumes: getNamelessVolumes;
In order to test my validator, I don't want to actually call AWS, nor do I want to have to change my production code. The mock-aws module solves this problem - I can easily mock the describeVolumes method in my test then call the tag validator as normal. This way I can use varying test data to fully test the getNamelessVolumes function, without relying on AWS services. First I setup my test data:
Now I can write my test, mocking describeVolumes() to return my test data
var should = ;var AWS = ; // include mock-aws instead of aws-sdkvar tagValidator = ; ;
The point here is that I can test the functionality of my tag-validator without making real calls to AWS and I never had to modify my production code in order to make it testable. mock-aws handled it for me.
I built this for myself but feel free to use, share, submit bugs / pull requests, suggest changes or features etc.