createMockClient (testUtils)

stripe-node · payments, mock-client, http-matching, test-utils, dev-tools

Creates a mock Stripe client for testing by matching HTTP method and path to predefined responses, enabling isolated API simulations.

export const createMockClient = (
  requests: Array<{method: string; path: string; response: string}>
): Stripe => {
  return getMockStripe({}, (method, _host, path, _4, _5, _6, _7, callback) => {
    const request = requests.find((r) => r.method == method && r.path == path);
    if (!request) {
      throw new Error(`Unable to find a mock request for ${method} ${path}`);
    }

    callback(null, JSON.parse(request.response));
  });
};

See the full source, get the GitHub permalink, and search 40K more like it.

Get a free API key