#keirn_h-jest
1 messages · Page 1 of 1 (latest)
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 |```
Hi there
Give me a moment to take a look
sure
If you debug and step through that test is the function actually called?
Yes. I am getting the actual payment intent from Stripe instead of the mock result.
Oh I see. Looking in to how to redirect to your mock endpoint, I think the stripe library has a setting for that
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.
Oh I assumed you were using the official stripe-mock https://github.com/stripe/stripe-mock
No I was not using that.
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?
If that can be implemented on my local device, then yes, that would be a solution.
From what I can see on Stack Overflow you can do this with just Jest if you want, though we are less familiar with jest https://stackoverflow.com/questions/55521585/mock-stripe-with-jest
Though yes, stripe-mock runs a local server on your device. So technically it will be networked but all within localhost
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?
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
keirn_h-jest
anyone in your team more experienced in jest unit testing?
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.