简体   繁体   中英

Twilio API mocking unit testing in Nestjs using Jest

I have this twilio.service.ts file. I want to write a unit test in twilio.service.spec.ts for two cases in which the verification OTP has been sent successfully or failed with missing parameters.

twilio.services.ts :

  async startVerification(
   startVerificationDto: StartVerificationDto
   ) {
    try {
      const verificationResponse = await this.twilioClient.verify
        .services("TWILIO_VERIFY_SERVICE_SID")
        .verifications.create({
          to: `+91${sendVerificationCodeDto.phoneNumber}`,
          channel: "sms",
          locale: "en",
        });
      return verificationResponse;
    } catch (error) {
      throw new BadRequestException("Failed sending OTP");
    }
  }

It tried so far:

it("Should be able to send verification code on mobile number", async () => {
    const actualRes = await controller.startVerification({
      phoneNumber: "xxxxxxxxxxx",
    });

    expect(twilioService.twilioClient.verify.services).toBeCalledWith(
      "TWILIO_VERIFY_SERVICE_SID"
    );

    expect(actualRes).toEqual({
      sid: "VA_TWILIO_VERIFY_SERVICE_SID",
    });
  });



  it("Should return error if required parameters are missing", async () => {

    expect(twilioService.twilioClient.verify.services).not.toBeCalledWith(
      "TWILIO_VERIFY_SERVICE_SID"
    );

    try {
      await controller.startVerification({
        phoneNumber: "xxxxxxxxxxx",
      });
    } catch (e) {
      expect(e.message).toBe("Failed sending OTP");
    }
  });

My main concern is: how to mock nested functions of Twilio (verify>service>verification>create). Also, after mocking how do I call or use that in spec.ts file.

Twilio developer evangelist here.

Mocking nested objects and function calls is more annoying than difficult. I think something like this should work for you:

(I'm not sure the relationship between the controller and the twilioService in your example. I've tried to mock the twilioClient on the twilioService here, but you may need to adjust that based on how things fit together.)

it("Should be able to send verification code on mobile number", async () => {
  const mockVerificationCreate = jest.fn().mockResolvedValue({
    sid: "VA_TWILIO_VERIFY_SERVICE_SID"
  })
  const mockVerifyService = jest.fn().mockReturnValue({
    verifications: {
      create: mockVerificationCreate
    }
  })
  twilioService.twilioClient = {
    verify: {
      services: mockVerifyService
    }
  }
 
  const actualRes = await controller.startVerification({
    phoneNumber: "xxxxxxxxxxx",
  });

  expect(twilioService.twilioClient.verify.services).toBeHaveBeenCalledWith(
    "TWILIO_VERIFY_SERVICE_SID"
  );
  expect(mockVerificationCreate).toHaveBeenCalledWith({
    to: "+91xxxxxxxxxxx",
    channel: "sms",
    locale: "en",
  });
  expect(actualRes).toEqual({
    sid: "VA_TWILIO_VERIFY_SERVICE_SID",
  });
});

Let me know if that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM