I have a simple Handler with calls getData defined in separate file
export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
let respData = await new DynamoDBClient().getData(123);
return {
statusCode: 200,
body: JSON.stringify(respData),
};
};
within my DynamoDB class, I have the following.
import { DynamoDB } from 'aws-sdk';
export default class DynamoDBClient {
private config: Config;
private client: DynamoDB.DocumentClient;
constructor() {
this.config = getConfig();
const dynamoDBClientConfig = this.config.mockDynamoDBEndpoint
? {
endpoint: this.config.mockDynamoDBEndpoint,
sslEnabled: false,
region: 'local'
}
: undefined;
this.client = new DynamoDB.DocumentClient(dynamoDBClientConfig);
}
// function
getData= async (id: string): Promise<any> => {
const response = await this.client
.query({
TableName: tableName,
IndexName: tableIndex,
KeyConditionExpression: 'id= :id',
ExpressionAttributeValues: {
':id': id
}
})
.promise();
return response;
}
}
My Test case
describe('DynamoDB', () => {
test('should return no data', async () => {
const spy = jest.spyOn(DynamoDBClient, 'getData').mockImplementation(() => jest.fn(() => {
return Promise.resolve({});
}));
const actual = await handler(event);
console.log(actual);
expect(actual).toEqual({ statusCode: 400, body: JSON.stringify({ }) });
});
});