aws-sdk-mock | js aws-sdk | Cloud Functions library
kandi X-RAY | aws-sdk-mock Summary
kandi X-RAY | aws-sdk-mock Summary
AWSome mocks for Javascript 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. If you are new to Amazon WebServices Lambda (or need a refresher), please checkout our our Beginners Guide to AWS Lambda:
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Mocks a stub method .
- wrap replace method
- Mocks a handler for a service .
- Restores a method on a service instance
- Restore a given service
- Restores all services in the given array
- Restores all methods on a service .
aws-sdk-mock Key Features
aws-sdk-mock Examples and Code Snippets
async getUser(email) {
// instantiate the service inside the function being tested in order for aws-sdk-mock
// to mock it successfully
if(process.env.NODE_ENV === 'test')
docClient = new AWS.DynamoDB.DocumentClient()
const par
Community Discussions
Trending Discussions on aws-sdk-mock
QUESTION
I'm trying to archive an entire folder using Node, archiver and fs, but nothing seems to happen.
This is my implementation:
...ANSWER
Answered 2021-Oct-20 at 15:09Issue was caused because I did not create the directory to where I wanted to store the zip file. Once I added the directory creation at startup everything worked.
QUESTION
I have a database file (not a class) that does an update operation as shown below -
...ANSWER
Answered 2021-Oct-18 at 05:41proxyquire
and sinon
packages are enough, you don't need to use aws-sdk-mock
package.
index.ts
:
QUESTION
I have decided to go with mocking dynamodb in my express API using jest.
Checkedout out various examples aws-sdk-mock https://stackoverflow.com/a/64568024/13126651
but what i am failing to understand is that -> "dynamodb operations code is inside express route and test file is seperate, so how do i write a mock which ensures that does testing express route code but uses mock which i am implementing. "
my test.js
...ANSWER
Answered 2021-Aug-24 at 04:49You can use jest.doMock(moduleName, factory, options) to mock aws-sdk
module.
Since the DocumentClient
class is initialized in the module scope, the module will be cached if the same module is required for multiple times, which means that mock objects in the module scope will also be cached. In order to clear the cache, you need to use the jest.resetModules()
method to ensure that the test cases are isolated, and the mock object of each test case will not affect other test cases.
I made some changes to the code to keep it simple.
E.g.
server.js
:
QUESTION
I'm having an issue while trying to mock lambda.invoke
which I'm calling from within another lambda function.
- The function is wrapped (I can't use sinon after, it will tell me it's already wrapped).
- The test keeps calling the lambda function on AWS instead of calling the mock.
- It does the same if I use sinon instead of aws-sdk-mock.
test.js
ANSWER
Answered 2021-Aug-04 at 08:36It is not entirely clear from the code you have shared, but presumably, you have a reference to lambda
in your handler.js
before you have wrapped the function in your test. Could you add the const lambda = new AWS.Lamda({})
line inside your handler function instead of outside of it?
QUESTION
I'm using this wrapper function to upload objects to s3
...ANSWER
Answered 2021-Apr-22 at 04:53You don't need to use aws-sdk-mock
package. You can use jest.mock(moduleName, factory, options) method to mock aws-sdk
package by yourself.
E.g.
upload.js
:
QUESTION
I work on a project where we use DynamoDb to fetch data from it.
One of our queries uses an :id
parameter to filter the records:
ANSWER
Answered 2021-Apr-08 at 11:52We have ended up to use optional chaining like this:
QUESTION
I'm having some trouble using the aws-sdk-mock
library and Typescript using ts-jest
. I'm running the example test from the aws-sdk-mock homepage, shown below. However when I run this test using ts-jest
I get the following exception:
ANSWER
Answered 2021-Mar-03 at 11:26I found the answer myself by accident however I do not know why this is the case.
I changed my tsconfig.json
, setting esModuleInterop
to false
and all my problems went away.
QUESTION
I have Connection class that is used to connect to AWS Rds Proxy via IAM Authentication. Part of that process is to create a token. I have a function to create the token but now I having a hard time to mock and test it.
Here is the Connection class with setToken
method:
ANSWER
Answered 2020-Oct-03 at 04:25The AWS
instance/object in your test scope is different from the AWS
instance/object being used in your setToken
method.
aws-sdk-mock
mocks this instance
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.
Also require
will return a new instance.
In essence you are mocking an instance in your test while your actual code is using another instance that has not been mocked.
Possible Solutions Solution 1You could modify your code to allow you to optionally inject the desired AWS
instances to use eg
QUESTION
I'm working on a simple AWS lambda function which is triggered by DynamoDB Streams events and should forward all records except for REMOVE
events to an SQS queue. The function works as expected, no surprises there.
I want to write a unit test to test the behavior of not submitting anything to SQS when it's a DELETE
event. I first tried this using aws-sdk-mock. As you can see in the function code, I try to comply with lambda best practices by initializing the SQS client outside of the handler code. Apparently this prevents aws-sdk-mock from being able to mock the SQS service (there is an issue on GitHub regarding this: https://github.com/dwyl/aws-sdk-mock/issues/206).
I then tried to mock SQS using jest which required more code to get it right, but I ended up with the same problem, being required to place the initialization of SQS inside the handler function which violates lambda best practices.
How can I write a unit test for this function while letting the initialization of the SQS client (const sqs: SQS = new SQS()
) outside the handler? Am I mocking the service the wrong way or has the structure of the handler to be changed in order to make it easier to test?
I'm aware that this lambda function is pretty straight forward and unit tests might be unnecessary, but I will have to write further lambdas with more complex logic and I think this one is quite suitable to demonstrate the problem.
index.ts
...ANSWER
Answered 2020-Oct-02 at 12:35Just a rough idea of how would I approach this:
- Instead of doing actual SQS sending/manipulation inside the main function, I would create an interface for message client. Something like this:
QUESTION
I am having a Typescript backend structure and I want to create unit tests for all the functionalities. I am using JEST and aws-skd-mock for mocking AWS. I have tried some things but it seems I am not doing the right thing.
I have this service where I am getting a parameter from ParamterStore (amazon.service.ts):
...ANSWER
Answered 2020-May-19 at 03:47You don't need to mock ./amazon.service.ts
module. Here is the unit test solution without using aws-sdk-mock
.
E.g.
amazon.service.ts
:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install aws-sdk-mock
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page