Mock Razorpay API using Jest
Mock Razorpay API using Jest

Mock Razorpay API using Jest

Spread the love
Mock Razorpay API using Jest
Automated testing

Jest is one of the most popular test frameworks used in JavaScript. By writing test cases you can be sure that the newly added feature would be bug-free and won’t affect your existing features. Which makes your application to be more reliable, efficient and also guarantees the quality of an application. The use of third-party services and libraries is a common part of modern projects. It’s easy to test the code written in your project because you have full control over it. But it’s difficult to control the behavior of third-party services and to solve this problem, we mock the third-party service.

Also, as a best practice in testing, you should only be testing your code, not code imported from other places. Ideally, test cases written by the vendor should cover that part. Mocking simply allows you to replace the actual implementation with a fake/fixed set of the desired output. Razorpay is one of the popular payment gateways in India used by the developer for online payment.

Let’s see how we can mock the Razorpay API to test our APIs using Jest. If you don’t know how to setup jest, read here.

Step 1: API implementation

Below is the code, which shows the actual implementation of the Razorpay API in index.js. To know more about the Razorpay integration with Node.js, read here.

const Razorpay = require('razorpay')
const rzp = new Razorpay({
 key_id: "YOUR_KEY_ID",
 key_secret: "YOUR_KEY_SECRET",
})'/create_order', async (req, res) => {
 try {
  const rzpOrder = await rzp.orders.create({
   amount: amount * 100, // rzp format with paise
   currency: 'INR',
   receipt: "receipt#1" //Receipt no that corresponds to this Order,
   payment_capture: true,
   notes: {
    orderType: "Pre"
   } //Key-value pair used to store additional information
 } catch (err) {

Step 2: API mocking

Now let’s mock the Razorpay module completely by returning the fixed set of output on each Razorpay API implementation.

jest.doMock('razorpay', () => {
return jest.fn(() => ({
  customers: {
   create: jest.fn(() => Promise.resolve({
    id: 'cust_123',
    name: "Jest_User",
    currency: "sgd",
    description: "Jest User Account created",
  orders: {
   create: jest.fn(() => Promise.resolve({
    id: '7JS8SH'
   fetchPayments: jest.fn(() => Promise.resolve([{
  subscriptions: {
   fetch: jest.fn(() => Promise.resolve({
    id: 'sub_00000000000001',
    "entity": "subscription",
    "plan_id": "plan_00000000000001",
   cancel: jest.fn(() => Promise.resolve({
    "id": "sub_00000000000001",
    "entity": "subscription",

After mocking the Razorpay, let’s write the test case into the index.test.js file and check the create account API.

const request = require('supertest')
const app = require('../../app')
const client = request(app)
const urlPrefix = '/'
let tokendescribe('Create order', () => {
 it('should create order', async () => {
  const res = await`${urlPrefix}/create_order`)
     amount: "2200",
     currency: "inr",
     partial_payment: true,

When you run the above test file you will receive the id of the order as “7JS8SH” instead of “Actual order Id” because you have mocked the Razorpay API. You can learn more about Razorpay & available features in official documentation.

Thanks for reading. If you liked the post, feel free to hit the clap button or leave a comment.

One comment

  1. Pingback: Payment Gateway - RazorPay integration with Node.js | Noob2Geek

Leave a Reply

Your email address will not be published. Required fields are marked *