3

I am using an Amazon DynamoDB package from aws-sdk v3 for javascript.
Here is the docs which I have followed: https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/dynamodb-examples.html
I have installed "@aws-sdk/client-dynamodb" package to perform CRUD operations from code.
I have imported commands from package in this way:

import { DynamoDBClient, PutItemCommand, DeleteItemCommand, UpdateItemCommand, GetItemCommand } from "@aws-sdk/client-dynamodb"; const dynamodbClient = new DynamoDBClient({ region: process.env.DYNAMODB_REGION, endpoint: process.env.DYNAMODB_ENDPOINT }); const result = await dynamodbClient.send(new PutItemCommand(params)); 

I have tried to mock Amazon DynamoDB following Jest docs but it was calling real Amazon DynamoDB in local.

How to mock these "@aws-sdk/client-dynamodb" package in Nodejs?
Please provide an example in Nodejs!

1
  • if you are looking to write unit test cases for this, You will need to build a wrapper over dynamoDb and mock that wrapper layer. one way to implement that wrapper layer is to build a Repository pattern. Mocking Dynamodb is not impossible but mocking your own wrapper layer would be far more easier. Commented Jun 17, 2021 at 1:22

4 Answers 4

2

AWS recommends mocking DynamoDB and the rest of AWS with aws-sdk-client-mock with documentation included with the module.

Install aws-sdk-client-mock and probably aws-sdk-client-mock-jest

npm i aws-sdk-client-mock aws-sdk-client-mock-jest 

Import the module

import {mockClient} from "aws-sdk-client-mock"; import 'aws-sdk-client-mock-jest'; 

Create a mock. Reset after each test

describe('Winner purge service', () => { const ddbMock = mockClient(DynamoDBClient) beforeEach(() => { jest.clearAllMocks() ddbMock.reset() }) 

Define responses

 ddbMock .on(ScanCommand).resolvesOnce({ Items: [marshall(configItem, {removeUndefinedValues: true})], Count: 1 }) ddbMock.on(QueryCommand) .resolvesOnce({ Items: [marshall(olderWinner)], Count: 1 }) .resolves({ Items: [], Count: 0 }) 

Test calls

expect(ddbMock.commandCalls(DeleteItemCommand).length).toBe(0) 
Sign up to request clarification or add additional context in comments.

10 Comments

I agree with this.
My issues comes with an error where it says resource not found when you do not add endpoint and region into it
Not mocking everything AWS?
added stackoverflow question if you fancy answering there ...................... stackoverflow.com/questions/76354962/…
I'm getting also the resource, region issue, as if it wasn't really getting mocked
|
1

This answer may come a bit late :) Currently there are two very useful package to deal with aws dynamodb mocks

I my case, I ended up using the second option, jest-dynalite since it doesnt require java and its very easy to configure :)

Comments

1

You can use AWSMock along with sinon and mock the method and response as you want. It works really great with jest and it doesn't call a real database.

Comments

0

You should install the aws dynamodb jar and use an endpoint with your dynamodb client.

You can setup a docker container to run dynamodb:

$ docker run -d -p 8000:8000 instructure/dynamo-local-admin:latest 

When you setup the dynamodb client for your tests, keep using the endpoint:

import { DynamoDBClient, PutItemCommand, DeleteItemCommand, UpdateItemCommand, GetItemCommand } from "@aws-sdk/client-dynamodb"; const dynamodbClient = new DynamoDBClient({ region: process.env.DYNAMODB_REGION, endpoint: process.env.DYNAMODB_ENDPOINT }); const result = await dynamodbClient.send(new PutItemCommand(params)); 

After you run your tests, you can check the results on the admin UI available at localhost:8000.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.