在 Node 打字稿中用玩笑模拟 Stripe API

时间:2021-06-29 13:34:51

标签: node.js typescript testing jestjs stripe-payments

我一直在尝试模拟 Stripe API 以执行测试。我对用玩笑模拟函数的经验很少,但我已经对如何模拟 Stripe 的 API 进行了深入搜索,但似乎没有奏效。

我的文件结构如下:

src/payment-gateways/stripe.ts

import Stripe from 'stripe'
import { PAYMENT_STRIPE_API_SECRET_KEY } from '../config'

const stripe = new Stripe(PAYMENT_STRIPE_API_SECRET_KEY, {
  apiVersion: '2020-08-27',
})

export default stripe

然后在我的 stripe.test.ts 上,我调用我的 API 端点,以便在逻辑中间创建一个条带客户。

我已经尝试过的是:

src/tests/__mocks__/stripe.ts

export class Stripe {}
const stripe = jest.fn(() => new Stripe())


export default stripe

src/...../stripe.test.ts

import { Stripe } from 'stripe'

Stripe.prototype.customers = {
    create: jest.fn(() => ({
              id: 1,
           })),
  } as unknown as Stripe.CustomersResource

//And also tried this without __mocks__

jest.mock('../../../../../../payment-gateways/stripe', () => {
  return jest.fn(() => ({
    customers: {
      create: jest.fn(() =>
        Promise.resolve({
          id: '1',
        })
      ),
    },
  }))
})

describe('stripe workflow', ()=> {
   it('creates a customer', async () => {
      await apollo.mutate...
   .
   .
   .
 })
})

但我不断收到错误 [[GraphQLError: Cannot read property 'create' of undefined]] 两种方法。

我想我在 jest 使用模拟的过程中遗漏了一些东西

0 个答案:

没有答案