【发布时间】:2022-01-21 14:29:33
【问题描述】:
我不是 100% 确定我的术语就在这里,但我想做的是如下。 我通过创建这样的手动模拟成功地模拟了 stripe.customers.create:
__mocks__/stripe.js
class Stripe {}
const stripe = jest.fn(() => new Stripe());
module.exports = stripe;
module.exports.Stripe = Stripe;
然后在我的 unit.test.js 中我像这样导入模拟
const { Stripe } = require("stripe");
Stripe.prototype.customers = {
create: jest.fn(() => {
return {
id: "cus_testid",
};
}),
};
stripe.customers.create() 方法被模拟得很好。然而,当我开始(因为没有更好的词)“嵌套”或“链接”这些方法时,问题就开始了。例如,这个模拟将不起作用:
Stripe.prototype.checkout.sessions = {
create: jest.fn(() => {
return {
session: {
url: 'someurl'
}
}
})
}
TypeError:无法设置未定义的属性“会话”
为什么我不能设置这样的嵌套方法模拟,我应该如何处理这样的嵌套模拟?我应该修改手动模拟来完成这个吗?
【问题讨论】:
-
你是如何使用 mocked stripe api 的?
-
如果你的意思是原始实现,其中 Stripe API 被模拟,然后像这样(我使用 billingPortal 而不是 checkout,因为语法更短): const portalSession = await stripe.billingPortal.sessions .create({ customer: stripeCustomerId, return_url: returnUrl, });
标签: javascript node.js unit-testing jestjs stripe-payments