MSW无法捕获Stripe请求。

huangapple go评论67阅读模式
英文:

MSW doesn't catch Stripe requests

问题

I'm trying to use MSW to mock my server side calls to Stripe in my integration tests.

The code is pretty straight forward:

// @vitest-environment node
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { describe, expect, test } from 'vitest';

import {
  mockStripePrices,
  mockStripeProducts,
} from '~/test/mocks/fixtures/stripe';

import { fetchProductsWithPrices } from './billing-helpers.server';

const server = setupServer(
  rest.get('https://api.stripe.com/v1/products', (request, response, context) =>
    response(context.json(mockStripeProducts)),
  ),
  rest.get('https://api.stripe.com/v1/prices', (request, response, context) =>
    response(context.json(mockStripePrices)),
  ),
);

beforeAll(() => server.listen({ onUnhandledRequest: 'warn' }));
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

describe('fetchProductsWithPrices()', () => {
  test('given nothing: returns all our products and their prices', async () => {
    const actual = await fetchProductsWithPrices();
    const expected = [];

    expect(actual).toEqual(expected);
  });
});

Where fetchProductsWithPrices is:

import { stripeAdmin } from './stripe-admin.server';

export async function fetchProductsWithPrices() {
  const [products, prices] = await Promise.all([
    stripeAdmin.products.list({ active: true }),
    stripeAdmin.prices.list({ active: true }),
  ]);

  return products.data.map(product => ({
    ...product,
    prices: prices.data.filter(price => price.product === product.id),
  }));
}

Somehow the real data is coming through, and NOT the mocks. (So my function works as expected, but MSW should catch the requests and return the mock data instead of my real data).

What is going wrong? Is it maybe a simple typo that I can't find?

I was under the impression that the Node.js Stripe library is simply a wrapper around Stripe's REST API, but just to make sure I wasn't tripping I also tried mocking graphql:

const stripe = graphql.link('https://api.stripe.com/v1/');

But that didn't work neither.

What am I doing wrong?

英文:

I'm trying to use MSW to mock my server side calls to Stripe in my integration tests.

The code is pretty straight forward:

// @vitest-environment node
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { describe, expect, test } from 'vitest';

import {
  mockStripePrices,
  mockStripeProducts,
} from '~/test/mocks/fixtures/stripe';

import { fetchProductsWithPrices } from './billing-helpers.server';

const server = setupServer(
  rest.get('https://api.stripe.com/v1/products', (request, response, context) =>
    response(context.json(mockStripeProducts)),
  ),
  rest.get('https://api.stripe.com/v1/prices', (request, response, context) =>
    response(context.json(mockStripePrices)),
  ),
);

beforeAll(() => server.listen({ onUnhandledRequest: 'warn' }));
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

describe('fetchProductsWithPrices()', () => {
  test('given nothing: returns all our products and their prices', async () => {
    const actual = await fetchProductsWithPrices();
    const expected = [];

    expect(actual).toEqual(expected);
  });
});

Where fetchProductsWithPrices is:

import { stripeAdmin } from './stripe-admin.server';

export async function fetchProductsWithPrices() {
  const [products, prices] = await Promise.all([
    stripeAdmin.products.list({ active: true }),
    stripeAdmin.prices.list({ active: true }),
  ]);

  return products.data.map(product => ({
    ...product,
    prices: prices.data.filter(price => price.product === product.id),
  }));
}

Somehow the real data is coming through, and NOT the mocks. (So my function works as expected, but MSW should catch the requests and return the mock data instead of my real data).

What is going wrong? Is it maybe a simple typo that I can't find?

I was under the impression that the Node.js Stripe library is simply a wrapper around Stripe's REST API, but just to make sure I wasn't tripping I also tried mocking graphql:

const stripe = graphql.link('https://api.stripe.com/v1/');

But that didn't work neither.

What am I doing wrong?

答案1

得分: 1

这是Stripe中的一个错误,将在即将发布的版本中修复:https://github.com/stripe/stripe-node/issues/1844#issuecomment-1648451436

英文:

This was a bug in Stripe that will be fixed in an upcoming version: https://github.com/stripe/stripe-node/issues/1844#issuecomment-1648451436

huangapple
  • 本文由 发表于 2023年7月13日 21:16:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定