gpt4 book ai didi

node.js - Jest 模拟 Stripe

转载 作者:搜寻专家 更新时间:2023-11-01 00:03:40 25 4
gpt4 key购买 nike

我想在 Jest 中模拟 Node Stripe SDK,因为我不想从 Stripe 运行模拟 API 服务器,但我不知道该怎么做。我正在创建一个 __mocks__ 目录并添加 stripe.js 但我无法获得任何可用于导出的内容。

在调用 strypegw.charges.create() 时,我通常会收到 TypeError: Cannot read property 'create' of undefined。我正在使用 ES6 模块语法,所以我 import stripe from 'stripe'

最佳答案

// your-code.js
const stripe = require('stripe')('key');
const customer = await stripe.customers.create({
...
});

// __mocks__/stripe.js
class Stripe {}
const stripe = jest.fn(() => new Stripe());

module.exports = stripe;
module.exports.Stripe = Stripe;

// stripe.tests.js
const { Stripe } = require('stripe');
const createCustomerMock = jest.fn(() => ({
id: 1,
...
}));
Stripe.prototype.customers = {
create: createCustomerMock,
};

关于node.js - Jest 模拟 Stripe ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55521585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com