Created
August 30, 2019 05:15
-
-
Save techieshark/151aeef6f4fc336738de0a661565d0b9 to your computer and use it in GitHub Desktop.
jest Stripe mock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// __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