From be845b710bb75c9ea1cb41601c1549ce93941723 Mon Sep 17 00:00:00 2001 From: soleil00 Date: Mon, 27 May 2024 15:47:15 +0200 Subject: [PATCH] mocked all email service calling during the test --- __test__/cart.test.ts | 5 +++++ __test__/payment.test.ts | 6 ++++++ __test__/product.test.ts | 5 +++++ __test__/user.test.ts | 21 ++++++++++++++++----- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/__test__/cart.test.ts b/__test__/cart.test.ts index e5b12f7..d553021 100644 --- a/__test__/cart.test.ts +++ b/__test__/cart.test.ts @@ -11,6 +11,11 @@ import { dummy } from "./prod"; import * as userServices from "../src/services/user.service" import { number } from "joi"; +jest.mock("../src/services/mail.service", () => ({ + sendEmailService: jest.fn(), + sendNotification: jest.fn(), +})); + const queryInterface = sequelize.getQueryInterface(); let sellerToken: any; diff --git a/__test__/payment.test.ts b/__test__/payment.test.ts index 0070c7e..d835f3f 100644 --- a/__test__/payment.test.ts +++ b/__test__/payment.test.ts @@ -15,6 +15,12 @@ let adminToken: any; let sellerToken: any; +jest.mock("../src/services/mail.service", () => ({ + sendEmailService: jest.fn(), + sendNotification: jest.fn(), +})); + + describe("test stripe api payment", () => { beforeAll(async () => { try { diff --git a/__test__/product.test.ts b/__test__/product.test.ts index 5b95c9e..53bdef7 100644 --- a/__test__/product.test.ts +++ b/__test__/product.test.ts @@ -17,6 +17,11 @@ import CartItem from "../src/sequelize/models/CartItem"; import OrderItem from "../src/sequelize/models/orderItems"; import * as userService from "../src/services/user.service" +jest.mock("../src/services/mail.service", () => ({ + sendEmailService: jest.fn(), + sendNotification: jest.fn(), +})); + const userData: any = { name: "yvanna", username: "testuser", diff --git a/__test__/user.test.ts b/__test__/user.test.ts index 08f7bdd..a905d82 100644 --- a/__test__/user.test.ts +++ b/__test__/user.test.ts @@ -18,7 +18,12 @@ import Redis from "ioredis"; import { env } from "../src/utils/env"; import { generateResetToken } from "../src/utils/generateResetToken"; -let redisClient:any; +let redisClient: any; + +jest.mock("../src/services/mail.service", () => ({ + sendEmailService: jest.fn(), + sendNotification: jest.fn(), +})); const userData: any = { @@ -32,7 +37,7 @@ const userData: any = { const dummySeller = { name: "dummy1234", username: "username1234", - email: "soleilcyber00@gmail.com", + email: "srukundo01@gmail.com", password: "1234567890", lastPasswordUpdateTime: "3000, 11, 18" }; @@ -233,15 +238,21 @@ describe("Testing user Routes", () => { }); - test("Should send otp verification code", async () => { + test("Should send otp verification code", async () => { + jest.unmock("../src/services/mail.service"); + const originalMailService = jest.requireActual("../src/services/mail.service"); const spy = jest.spyOn(mailServices, "sendEmailService"); const response = await request(app).post("/api/v1/users/login").send({ email: dummySeller.email, password: dummySeller.password, }); - expect(response.body.message).toBe("OTP verification code has been sent ,please use it to verify that it was you"); - // expect(spy).toHaveBeenCalled(); + expect(response.body.message).toBe("OTP verification code has been sent ,please use it to verify that it was you"); + expect(spy).toHaveBeenCalled() + jest.mock("../src/services/mail.service", () => ({ + sendEmailService: jest.fn(), + sendNotification: jest.fn(), + })); }, 70000); test("should log a user in to retrieve a token", async () => {