unit-testing - lambda 函数 : TypeError: stripe_1. 默认值不是构造函数上的模拟 Stripe 失败

标签 unit-testing jestjs mocking stripe-payments ts-jest

我想在 lambda 函数上模拟 stripe sdk,这就是我在配置上创建 stripe 实例的方式

export const stripe = new Stripe(STRIPE_SECRET_KEY, {
  apiVersion: '2020-08-27',
  typescript: true,
  maxNetworkRetries: 2,
});

这就是我在 lambda 函数上使用 stripe 实例的方式

import { stripe, STRIPE_TEST_USERS } from '../config';

await stripe.invoices.retrieve(id)

这就是我得到的

  TypeError: stripe_1.default is not a constructor

  36 | });
  37 |
> 38 | export const stripe = new Stripe(STRIPE_SECRET_KEY, {
     |                       ^
  39 |   apiVersion: '2020-08-27',
  40 |   typescript: true,
  41 |   maxNetworkRetries: 2,

别误会我的意思,我已经尝试了所有我能做的事情,我已经尝试过这个方法

第一

 jest.mock("stripe", () => {
      return jest.fn().mockImplementation(()=> {
        return {
          invoices: {
            retrieve: () =>jest.fn(),
          },
        };
      });
    });

第二

jest.mock('stripe', () =>
   jest.fn().mockImplementation(() => ({
    invoices: {
      retrieve: () => jest.fn()
   },
  }))
 );

第三

jest.mock('stripe', () => ({
  ...jest.mock('stripe'),
  Stripe: jest.fn().mockImplementation(() => {
    return {
      invoices: jest.fn().mockImplementation(() => {
        return {
          retrieve: jest.fn(),
        };
      }),
    };
  }),
}));

但是这些都不起作用,有什么解决办法吗?

最佳答案

我认为你可以使用这个,你需要先导入模块

jest.mock("stripe", () => {
 return {
    __esModule: true,
    default: jest.fn().mockImplementation(() => {
      return {
        subscriptions: {
          update: () => updateMock(),
        },
      };
    }),
  };
});

更新是你想要模拟的内容

关于unit-testing - lambda 函数 : TypeError: stripe_1. 默认值不是构造函数上的模拟 Stripe 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73789933/

相关文章:

visual-studio - BadImageException : 'Could not load file or assembly ' Microsoft. VisualStudio.QualityTools.VideoRecorderEngine'或其依赖项之一。

jestjs - 测试通过代理向后端发送请求的 react 应用程序

c# - 将 Moq 模拟对象传递给构造函数

reactjs - 导入 keras-js 将破坏相关功能和组件的单元测试

java - Mockito - 模拟具体类

c++ - 在使用 googletest 进行模拟时,如何为重载方法指定 internal::AnythingMatcher 的参数类型?

django - 在 Django Rest Framework 和单元测试中使用嵌套对象

mysql - 错误:ActiveRecord::StatementInvalid:Mysql2::Error:字段 'full_name' 没有默认值

python - 在 SQLAlchemy 应用程序的单元测试中使用 Alembic?

typescript - TypeError : this. graphInspector.insertEnhancerMetadataCache 不是一个函数