问题
I want to unit test this completely the code for my function is given below
function.js
async function sesSendEmail(message) {
var ses = new aws.SES({ apiVersion: "2020-12-01" });
var params = {
Source: "abc@gmail.com",
Template: "deviceUsageStatisticsEmailTemplate",
Destination: {
ToAddresses: ["xyz@gmail.com"]
},
TemplateData: message,
}
try {
let res = await ses.sendTemplatedEmail(params).promise()
console.log(res)
}
catch (err) {
console.log(err)
}
What i have tried in my tests so far:
function.test.js
test('should send templated email success', async () => {
jest.spyOn(console, 'log');
const mData = {};
ses.sendTemplatedEmail.mockImplementationOnce(async (params,callback) => {
callback(null,mData)
});
const message = 'mock message';
await index.sesSendEmail(message);
expect(aws.SES).toBeCalledWith({ apiVersion: '2020-12-01' });
expect(ses.sendTemplatedEmail).toBeCalledWith(
{
Source: 'abc@gmail.com',
Template: 'deviceUsageStatisticsEmailTemplate',
Destination: {
ToAddresses: ['xyz@gmail.com'],
},
TemplateData: message,
},
);
await expect(console.log).toBeCalledWith(mData);
});
test('should handle error', () => {
const arb = "network error"
ses.sendTemplatedEmail = jest.fn().mockImplementation(() => {
throw new Error(arb);
})
const message = 'mock message'
expect(() => { index.sesSendEmail(message) }).toThrow(arb);
});
});
PROBLEM:
It gives an error
expect(jest.fn()).toBeCalledWith(...expected)
- Expected
+ Received
- Object {}
+ [TypeError: ses.sendTemplatedEmail(...).promise is not a function],
I have tried variations in mockimplementations but to no avail.. any help/suggestion is highly appreciated :)
UPDATE
Tried requiring aws-sdk-mock
aws.mock('ses','sendTemplatedEmail',function(callback){callback(null,mData)})
but still getting error
TypeError: Cannot stub non-existent own property ses
回答1:
I would say mock the aws sdk itself and test your methods the way you normally do. Once such library is aws-sdk-mock
(https://www.npmjs.com/package/aws-sdk-mock)
Something like this
const sinon = require("sinon");
const AWS = require("aws-sdk-mock");
test("should send templated email success", async () => {
const sendEmailStub = sinon.stub().resolves("resolved");
AWS.mock("SES", "sendTemplatedEmail", sendEmailStub);
const response = await sesSendEmail("{\"hi\": \"bye\"}");
expect(sendEmailStub.calledOnce).toEqual(true);
expect(sendEmailStub.calledWith(
{
"Source": "abc@gmail.com",
"Template": "deviceUsageStatisticsEmailTemplate",
"Destination": {
"ToAddresses": ["xyz@gmail.com"]
},
"TemplateData": "{\"hi\": \"bye\"}"
})).toEqual(true);
expect(response).toEqual("resolved");
AWS.restore();
});
test("should send templated email failure", async () => {
const sendEmailStubError = sinon.stub().rejects("rejected");
AWS.mock("SES", "sendTemplatedEmail", sendEmailStubError);
const response = await sesSendEmail("{\"hi\": \"bye\"}");
expect(sendEmailStubError.calledOnce).toEqual(true);
expect(sendEmailStubError.calledWith(
{
"Source": "abc@gmail.com",
"Template": "deviceUsageStatisticsEmailTemplate",
"Destination": {
"ToAddresses": ["xyz@gmail.com"]
},
"TemplateData": "{\"hi\": \"bye\"}"
})).toEqual(true);
expect(response.name).toEqual("rejected");
AWS.restore();
});
Make sure from your original method, you are returning response and error.
来源:https://stackoverflow.com/questions/59987495/how-to-test-promise-methods-used-in-aws-built-in-methods-in-jest-node-js