Reputation: 259
I tried many things already, nothing works. How do I mock the promise return await this.getDbClient().put(params).promise();
Any help?
const car: Car = {
id: 892421
};
it('#DatabaseHandler should create car', async () => {
DatabaseHandler.getDbClient().put = jest.fn().mockImplementation(() => car);
const result = await DatabaseHandler.createCar(car); // error here
expect(result).toEqual(car);
});
DatabaseHandler
import { DynamoDB } from 'aws-sdk';
import { DocumentClient } from 'aws-sdk/clients/dynamodb';
import { Car } from '/opt/nodejs/models/car.interface';
const documentClient = new DynamoDB.DocumentClient();
export class DatabaseHandler {
private static tableName: string = 'cars';
static getDbClient = (): DynamoDB.DocumentClient => {
return documentClient;
}
static createCar = async (car: Car) => {
const params: DocumentClient.PutItemInput = {
TableName: this.tableName,
Item: car
}
await this.getDbClient().put(params).promise();
}
}
TypeError: this.getDbClient(...).put(...).promise is not a functionJest
Upvotes: 0
Views: 69
Reputation: 931
The put
method returns an object that has a promise
method you are not mocking that one.
Try this
DatabaseHandler.getDbClient().put = jest.fn().mockImplementation(() => {
// returning an object that has the promise method
return {
promise: () => Promise.resolve(car)
}
});
Upvotes: 1