#keirn_h-jest

1 messages · Page 1 of 1 (latest)

wintry oliveBOT
analog crown
#

I have a service defined as following.

#
  logger.info('CreateStripePaymentIntent');
  const stripeClient = await GetStripeClient();
  const paymentIntent = await stripeClient.paymentIntents.create(createParams);
  return paymentIntent;
}```
#

and I would like to mock stripe paymentintent create api.

#

I have a jest test script as below.

#
describe('create-stripe-payment-intent', () => {
  test('Run CreateStripePaymentIntent service successfully', async () => {
    const mockPaymentIntentsCreate = jest.fn();
    jest.mock('Stripe', () => {
      const paymentIntents = jest.fn();
      // @ts-ignore
      paymentIntents.create = (param) = mockPaymentIntentsCreate(param);
      return jest.fn().mockImplementation(() => ({
        paymentIntents
      }));
    });  
    const paymentIntentsCreateResponse ={id: '123'};
    mockPaymentIntentsCreate.mockResolvedValue(mockPaymentIntents);
    await CreateStripePaymentIntent(paymentIntentParams);
    expect(mockPaymentIntentsCreate).toBeCalledTimes(1);
  });
});```
#

But for some reason, the mock is not being called.

#

    Expected number of calls: 1
    Received number of calls: 0

      167 |     mockPaymentIntentsCreate.mockResolvedValue(mockPaymentIntents);
      168 |     await CreateStripePaymentIntent(paymentIntentParams);
    > 169 |     expect(mockPaymentIntentsCreate).toBeCalledTimes(1);
          |                                      ^
      170 |   });
      171 | });
      172 |```
dark island
#

Hi there

analog crown
#

Could you help me why I'm getting such test result?

#

Hi!

dark island
#

Give me a moment to take a look

analog crown
#

sure

worthy ether
#

If you debug and step through that test is the function actually called?

analog crown
#

Yes. I am getting the actual payment intent from Stripe instead of the mock result.

worthy ether
#

Oh I see. Looking in to how to redirect to your mock endpoint, I think the stripe library has a setting for that

analog crown
#

Do you mean mock endpoint at Stripe side? I was looking for a way to mock the API, I did not intent to invoke any network traffic.

worthy ether
analog crown
#

No I was not using that.

worthy ether
#

When using that you have to specify in your client library that you want to use a different endpoint

#

Right, this seems to be a general jest question right?

analog crown
#

If that can be implemented on my local device, then yes, that would be a solution.

worthy ether
#

Though yes, stripe-mock runs a local server on your device. So technically it will be networked but all within localhost

analog crown
#

I have been reading several posts in Stack Overflow, and I've tried many different patterns of testing using jest, but I could not find any that works. Can you provide support on identifying what can I do to make my unit test work?

worthy ether
#

Unfortunately we aren't that familiar with jest on this server, just Stripe official libraries. You might be able to get better help if you go to jest forums or somewhere that specializes in jest specifically

#

We have some knowledge around frameworks that can use our libraries but unfortunately my colleagues that are online now and I don't have much knowledge on jest, so we don't have recommendations on how to make sure you are using the mock object here

buoyant tiger
#

keirn_h-jest

analog crown
#

anyone in your team more experienced in jest unit testing?

buoyant tiger
#

Hi 👋 no, my teammate that you were working with came and asked all of us earlier. We are not familiar with the Jest framework and are not sure what advice to provide here.