Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jest Stripe mock
// __mocks__/stripe.ts: Jest Mock for Stripe class
/**
* Fake a response from stripe.customers.list().
* @example
* mockStripeCustomerList(1) === { data: ['fake customer'], object: 'list', … }
* @param count number of fake customers to return
*/
export const mockStripeCustomerList = (count: number) => ({
data: (new Array(count)).fill('fake customer'),
object: 'list',
url: 'fake/v1/customers/',
});
const stripeCustomersList = () => mockStripeCustomerList(0);
const mock = jest.fn().mockImplementation(() => {
return {
customers: {
list: () => jest.fn().mockImplementation(stripeCustomersList),
},
};
});
export default mock;
// Some other useful reads:
// https://stackoverflow.com/questions/51495473/typescript-and-jest-avoiding-type-errors-on-mocked-functions
// https://github.com/stripe/stripe-mock
// https://jestjs.io/docs/en/es6-class-mocks
// https://stackoverflow.com/a/57499771/1024811 (stripe jest mock)
// https://github.com/maurocarrero/sinon-jest-cheatsheet#return-value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment