Skip to content

Instantly share code, notes, and snippets.

@techieshark
Created August 30, 2019 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techieshark/151aeef6f4fc336738de0a661565d0b9 to your computer and use it in GitHub Desktop.
Save techieshark/151aeef6f4fc336738de0a661565d0b9 to your computer and use it in GitHub Desktop.
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