【发布时间】:2021-11-27 05:37:27
【问题描述】:
我有一个简单的处理程序,调用 getData 在单独的文件中定义
export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
let respData = await new DynamoDBClient().getData(123);
return {
statusCode: 200,
body: JSON.stringify(respData),
};
};
在我的 DynamoDB 类中,我有以下内容。
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;
}
}
我的测试用例
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({ }) });
});
});
【问题讨论】:
标签: typescript unit-testing testing jestjs amazon-dynamodb