如何测试在Jest Node JS中内置方法的AWS中使用的.promise()方法

问题描述 投票:1回答:1

我想对此单元进行完全的单元测试,下面给出了我的函数的代码

function.js

async function sesSendEmail(message) {
var ses = new aws.SES({ apiVersion: "2020-12-01" });
var params = {
    Source: "[email protected]",
    Template: "deviceUsageStatisticsEmailTemplate",
    Destination: {
        ToAddresses: ["[email protected]"]
    },
    TemplateData: message,
}
try {
    let res = await ses.sendTemplatedEmail(params).promise()
    console.log(res)
}
catch (err) {
    console.log(err)
}

到目前为止我在测试中尝试过的内容:

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: '[email protected]',
                Template: 'deviceUsageStatisticsEmailTemplate',
                Destination: {
                    ToAddresses: ['[email protected]'],
                },
                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);
    });
});

问题:

给出错误

 expect(jest.fn()).toBeCalledWith(...expected)

- Expected
+ Received

- Object {}
+ [TypeError: ses.sendTemplatedEmail(...).promise is not a function],

我已经尝试过各种实现的变体,但无济于事。高度赞赏任何帮助/建议:)

UPDATE

尝试要求aws-sdk-mock

aws.mock('ses','sendTemplatedEmail',function(callback){callback(null,mData)})

但仍然出现错误

TypeError: Cannot stub non-existent own property ses

javascript node.js amazon-web-services jestjs async.js
1个回答
1
投票

我会说模拟aws sdk本身,并以通常的方式测试您的方法。一旦此类库为aws-sdk-mockhttps://www.npmjs.com/package/aws-sdk-mock

这样的东西

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": "[email protected]",
      "Template": "deviceUsageStatisticsEmailTemplate",
      "Destination": {
        "ToAddresses": ["[email protected]"]
      },
      "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": "[email protected]",
      "Template": "deviceUsageStatisticsEmailTemplate",
      "Destination": {
        "ToAddresses": ["[email protected]"]
      },
      "TemplateData": "{\"hi\": \"bye\"}"
    })).toEqual(true);
    expect(response.name).toEqual("rejected");
    AWS.restore();
});

请确保从您的原始方法返回的响应和错误。

© www.soinside.com 2019 - 2024. All rights reserved.