嘲笑条纹 [英] Jest to mock Stripe

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

问题描述

我想使用Jest测试我的nodejs代码.在我的代码中,我使用条纹.

I want to test my nodejs code using Jest. In my code I use stripe.

当需要条纹时,您必须使用此行

When requiring stripe you have to use this line

const stripe=require('stripe')("apikey");

以便能够访问条纹方法.

in order to be able to access the stripe methods.

很明显,这是我要模拟的库,但如果我这样做,则

Obviously that's the library I want to mock, but if I do

jest.mock('stripe');

我无法模拟所需的条带方法,就像在不将键传递给高阶函数的情况下执行需求一样.

I cannot mock the stripe methods I need as it's like doing a require without passing the key in the higher order function.

我周围找不到任何关联.

I could not find any correlation around.

有没有办法做到这一点?

Is there a way to achieve that?

推荐答案

我认为您需要构建整个Stripe SDK模拟.这是对我的atm起作用的东西:

I figure you'll need to build your entire Stripe SDK mock. This is what's working for me atm:

// Stripe SDK mock
jest.mock('stripe', () => {
    return jest.fn().mockImplementation(() => {
        return {
            skus: {
                retrieve: (sku, callback) => {
                    callback({}, {});
                }
            }
        };
    });
});

上面的模拟在调用 stripe.skus.retrieve(req.query.sku,function(err,sku){...}) sku 对象>

The above mock will return empty sku object when calling stripe.skus.retrieve(req.query.sku, function(err, sku) { ... })

其余的应该去找.如果希望此模拟根据输入返回不同种类的数据,则需要在模拟中实现该逻辑.

Same should go for the rest. If you want this mock to return different kinds of data depending on input, you'll need to implement that logic in the mock.

希望这会有所帮助,

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

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