I am trying to mock dynamodb calls using "aws-sdk-client-mock" library. My main service folder contains test case file called test.main-service.js and main file called index.js. Whereas I have a local dependency called dynamoDbUtil.js which contains logic to make dynamodb calls.
I linked local dependency using npm i
and npm link
, and then used the linked local dependencies in my main service folder.
With linked local module, I am unable to mock the dynamodb GetCommand call, means
const mockDbClient = mockClient(DynamoDBDocumentClient);
is actually not working. While running test case, it tries to make GetCommand call to actual dynamodb table. Tried searching on net and asked on Chatgpt for solutions, but none of them worked. The only way to make the mock calls work is to copy the local module into the main service folder and install the dependency directly there. However, if someone can suggest how to make it work through linked local module, it would be very helpful.
Structure of my codebase is as follows:
-- local-modules
-- @api
-- dynamo-util
-- dynamoUtil.js
-- package.json
-- Services
-- main-service
-- test
-- test.main-service.js
-- index.js
-- package.json
My local dependency is as follows:
dynamoUtil.js
import {DynamoDBClient} from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb';
const dynamoClient = new DynamoDBClient({
region: 'us-east-1'
});
const docClient = DynamoDBDocumentClient.from(dynamoClient);
const getTableItems = async (id) => {
const params = {
TableName: 'My-Demo-Table-sdkv3',
Key: {
Id: id
}
};
const getTableCommand = new GetCommand(params);
const result = await docClient.send(getTableCommand);
console.log(result);
return result;
}
// getTableItems();
export {getTableItems};
package.json (of dynamo-util)
{
"name": "@api/dynamo-util",
"version": "1.0.0",
"main": "dynamoUtil.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.744.0",
"@aws-sdk/lib-dynamodb": "^3.744.0"
}
}
My main service's test case file is as follows:
test.main-service.js
import { should } from 'chai';
const chaiShould = should();
import { mockClient } from 'aws-sdk-client-mock';
import { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb';
import { getItemsFromDB } from '../index.js';
describe('test case1', () => {
const mockDbClient = mockClient(DynamoDBDocumentClient);
beforeEach(() => {
mockDbClient.reset();
})
it('test case for dynamodb', async () => {
const id = 1;
mockDbClient.on(GetCommand).resolves(
{
Item: {
id: 1,
name: "koko",
age: 32
}
}
)
const item = await getItemsFromDB(id);
console.log(item);
})
});
Main service file's index.js:
index.js
import { getTableItems } from '@api/dynamo-util';
const getItemsFromDB = async (id) => {
// console.log(await getTableItems(id));
const data = await getTableItems(id);
return data.Item;
}
export { getItemsFromDB };
package.json (of main service folder)
{
"name": "main-service",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"scripts": {
"test": "nyc mocha --timeout=9000000"
},
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.750.0",
"@aws-sdk/lib-dynamodb": "^3.750.0",
"@api/dynamo-util": "file:./local-modules/@api/dynamo-util",
"aws-sdk-client-mock": "^4.1.0",
"chai": "^5.1.2",
"mocha": "^11.1.0",
"nyc": "^17.1.0"
},
"nyc": {
"exclude": [
"local-modules/**"
]
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}