【问题标题】:AWS Services Mocking using jest使用 jest 模拟 AWS 服务
【发布时间】:2021-09-13 21:58:27
【问题描述】:

我正在尝试在我的项目中模拟一些 AWS 服务,我已将它们放在文件夹 mocks

例如

 __mocks__
        |__ aws-sdk.ts

所以在 aws-sdk.ts 中

export const getFn = jest
  .fn()
  .mockImplementation(() => ({ promise: ssmGetParameterMockResponse }))

export class SSM {
  getParameter = getFn
}

aws_sdk.SSM = SSM
module.exports = aws_sdk

ssmGetParameterMockResponse 在另一个模拟数据文件中,其值为

export const ssmGetParameterMockResponse = jest.fn().mockReturnValue(
  Promise.resolve({
    Parameter: {
      Value: JSON.stringify({
        Endpoint: 'http://abcdef.com',
        AppToken: 'AppToken',
        UserToken: 'UserToken',
        Name: 'Bleh',
      }),
    },
  })
)

在我的 ts 文件中

const ssm = new AWS.SSM()
const credentials = ssm
  .getParameter({
    Name: String(CRED),
  })
  .promise()
  .then(({ Parameter }) => JSON.parse(Parameter?.Value || '{}'))
const { Endpoint, UserToken, AppToken, Name } = await credentials

在我的测试文件中,当我尝试这个测试用例时,一切正常,我的模拟数据来自我的 mocks 文件夹,该文件夹使用来自 ssmGetParameterMockResponse 的数据,但是有一些测试用例我必须覆盖从 ssmGetParameterMockResponse 提供的值,我的意思是我必须删除属性“名称”以检查代码是否失败并正确捕获它们,

我在我的特定测试用例中尝试了以下方法,我想删除 name 属性

    const spyyy = jest.spyOn(mock, 'ssmGetParameterMockResponse')
    spyyy.mockReturnValue(
      Promise.resolve({
        Parameter: {
          Value: JSON.stringify({
            Endpoint: 'http://abcdef.com',
            AppToken: 'AppToken',
            UserToken: 'UserToken',
          }),
        },
      })
    )

但是我仍然无法正确获取详细信息,还有其他方法可以覆盖模拟吗???

【问题讨论】:

    标签: typescript amazon-web-services unit-testing mocking ts-jest


    【解决方案1】:

    我认为您遇到的问题是使用jest.spyOn。由于您已经在使用模拟函数,因此没有必要这样做。而只是给模拟函数一个新的模拟返回值。

    ssmGetParameterMockResponse.mockReturnValue(
      Promise.resolve({
        Parameter: {
          Value: JSON.stringify({
            Endpoint: 'http://abcdef.com',
            AppToken: 'AppToken',
            Name: 'Bleh',
          }),
        },
      })
    );
    

    【讨论】:

      【解决方案2】:

      我发现从他们自己的模块中导出 AWS 客户端更容易,这样更容易模拟

      以下示例。

      // amazonClients.js

      import * as AWS from "aws-sdk";
      
      export const ssm = new AWS.SSM();
      

      // index.js

      import { ssm } from "./amazonClients" 
      
      export const getCredentials = async () => {
        return ssm.getParameter({ Name: String(CRED)}).promise()
      } 
      

      // index.test.js

      import {getCredentials} from "./index";
      import {ssm} from "./amazonClients";
      
      jest.mock("./amazonClients");
      
      
      test("should return cred", async() => {
       ssm.mockReturnValue({ promise: () => Promise.resolve(STUB_SECRET) })
      
       const credentials = await getCredentials()
       
       expect(credentials.Name).toEqual("foo")
       expect(ssm).toHaveBeenCalled()
      })
      

      【讨论】:

        猜你喜欢
        • 2023-01-30
        • 2020-08-22
        • 1970-01-01
        • 2023-03-10
        • 2020-06-01
        • 2021-09-14
        • 2022-06-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多