Test Stripe with MockServer

kasir-barati

Mohammad Jawad (Kasir) Barati

Posted on September 2, 2024

Test Stripe with MockServer

Hey folks,

Have not been able to write integration test for your integrated Stripe in your NodeJS app? Or perhapse you've used things like mock-stripe and ain't particularly satisfied with the amount of freedom.

So today is your lucky day since I've created this simple app in NodeJS which I am testing Stripe with Jest. Bear in mind that here we are literally mocking the whole communication with Stripe therefore you need to still have some sort of IDK E2E tests or other integration tests where you do not mock the stripe-node communication with the real Stripe API.

Use a different env variable than NODE_ENV

You can do that of course by adding a new env variable which let you know when you should not mock it. If I where in that situation I would have probably do something about it where I am creating the stripe config. Or instantiating Stripe instance. Here you can see an implementation of it, there I am actually mocking it when I wanna mock it (in the payment-service integration tests) and also send a request to the actual Stripe api (in the integration tests written for the reservation-service).

Link to the simple implementation

But for now let me share the repo's link: https://github.com/kasir-barati/docker/tree/docker-compose/docker-compose-files/mockserver-integration-test

Read the README.md to learn how you should spin it up. BTW I truly like to hear from you and also if this was helpful in anyway consider giving my repo and this post a star and heart respectively.

Before I forget it I wanna also share this infographic I made from MockServer and how it works.

For each request received the following steps happen: first. find matching expectation and perform action. If no matching expectation proxy request if it is configured to act like a proxy request. Otherwise return 404

Just one more thing. I am not sure about proxying the requests back grom MockServer to Stripe. But if you have done it already do not hesitate to share it here or in my GH repo.

💖 💪 🙅 🚩
kasir-barati
Mohammad Jawad (Kasir) Barati

Posted on September 2, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Test Stripe with MockServer
testing Test Stripe with MockServer

September 2, 2024