我想在 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/