Skip to content

Instantly share code, notes, and snippets.

@techieshark
Created August 30, 2019 05:15
Show Gist options
  • Select an option

  • Save techieshark/151aeef6f4fc336738de0a661565d0b9 to your computer and use it in GitHub Desktop.

Select an option

Save techieshark/151aeef6f4fc336738de0a661565d0b9 to your computer and use it in GitHub Desktop.

Revisions

  1. techieshark created this gist Aug 30, 2019.
    32 changes: 32 additions & 0 deletions __mocks__stripe.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    // __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