开玩笑的模拟条纹 [英] Mock stripe with Jest

查看:62
本文介绍了开玩笑的模拟条纹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Jest中模拟节点Stripe SDK,因为我不想从Stripe运行模拟API服务器,但是我不知道如何去做.我正在创建__mocks__目录并添加stripe.js,但是我无法导出任何可用内容.

I'd like to mock the node Stripe SDK in Jest because I don't want to run the mock API server from Stripe but I can't figure how how to do it. I'm creating a __mocks__ directory and adding stripe.js but I can't get anything usable to export.

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

I typically get TypeError: Cannot read property 'create' of undefined when calling strypegw.charges.create(). I'm using ES6 module syntax so I 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,
};

这篇关于开玩笑的模拟条纹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆