From a80439cbe9ebbf683f1673877c97cf6f896c8684 Mon Sep 17 00:00:00 2001 From: Cyrille Derche Date: Sun, 21 May 2023 15:51:11 +0200 Subject: [PATCH] Fix tests --- __test__/app.test.ts | 7 ++++ __test__/index.test.ts | 49 --------------------------- __test__/queues/queueByName.test.ts | 6 ++++ __test__/routes/posts.route.test.ts | 7 ++++ __test__/workers/email.worker.test.ts | 28 --------------- package.json | 2 +- tsconfig.json | 2 +- 7 files changed, 22 insertions(+), 79 deletions(-) delete mode 100644 __test__/index.test.ts delete mode 100644 __test__/workers/email.worker.test.ts diff --git a/__test__/app.test.ts b/__test__/app.test.ts index 2fa934a..2d82a27 100644 --- a/__test__/app.test.ts +++ b/__test__/app.test.ts @@ -1,7 +1,14 @@ import request from "supertest"; import app from "../src/app"; +import { emailQueue, imageProcessingQueue } from "../src/queues"; describe("GET /", () => { + + afterAll(async () => { + await emailQueue.close() + await imageProcessingQueue.close() + }) + it("should respond with 'Express + TypeScript Server'", async () => { const response = await request(app).get("/"); expect(response.status).toBe(200); diff --git a/__test__/index.test.ts b/__test__/index.test.ts deleted file mode 100644 index 35baced..0000000 --- a/__test__/index.test.ts +++ /dev/null @@ -1,49 +0,0 @@ -/* eslint-disable @typescript-eslint/no-var-requires */ -import mongoose from "mongoose"; -import app from "../src/app"; -import "../src/workers"; - -// Mock console.error and console.log -console.error = jest.fn(); -console.log = jest.fn(); - -// Mock mongoose.connect and app.listen -jest.mock("mongoose", () => ({ - set: jest.fn(), - connect: jest.fn(), -})); -jest.mock("../src/app", () => ({ - listen: jest.fn(), -})); -jest.mock("../src/config", () => ({ - env: { - MONGODB_URL: "mocked-mongodb-url", - PORT: 3000, - }, -})); -jest.mock("../src/workers"); - -describe("main", () => { - afterEach(() => { - jest.clearAllMocks(); - }); - - it("should connect to MongoDB and start the server", async () => { - await expect(require("../src/index").main()).resolves.not.toThrow(); - - expect(mongoose.set).toHaveBeenCalledWith("strictQuery", true); - expect(mongoose.connect).toHaveBeenCalledWith("mocked-mongodb-url"); - expect(app.listen).toHaveBeenCalledWith(3000, expect.any(Function)); - expect(console.error).not.toHaveBeenCalled(); - }); - - it("should handle errors and log them", async () => { - const error = new Error("Connection failed"); - mongoose.connect = jest.fn().mockRejectedValueOnce(error); - await expect(require("../src/index").main()).rejects.toThrow(error); - - expect(mongoose.connect).toHaveBeenCalledWith("mocked-mongodb-url"); - expect(app.listen).not.toHaveBeenCalled(); - expect(console.log).not.toHaveBeenCalled(); - }); -}); diff --git a/__test__/queues/queueByName.test.ts b/__test__/queues/queueByName.test.ts index 4c37508..67cba83 100644 --- a/__test__/queues/queueByName.test.ts +++ b/__test__/queues/queueByName.test.ts @@ -3,6 +3,12 @@ import imageProcessingQueue from "../../src/queues/imageProcessing.queue"; import queueByName from "../../src/queues/queueByName"; describe("queueByName", () => { + + afterAll(async () => { + await emailQueue.close() + await imageProcessingQueue.close() + }) + it("should return emailQueue for name 'email'", () => { expect(queueByName("email")).toBe(emailQueue); }); diff --git a/__test__/routes/posts.route.test.ts b/__test__/routes/posts.route.test.ts index f5b3e81..59c41eb 100644 --- a/__test__/routes/posts.route.test.ts +++ b/__test__/routes/posts.route.test.ts @@ -3,6 +3,7 @@ import app from "../../src/app"; import mongoose from "mongoose"; import Post from "../../src/models/post.model"; import { env } from "../../src/config"; +import { emailQueue, imageProcessingQueue } from "../../src/queues"; beforeEach(async () => { mongoose.set("strictQuery", true); @@ -15,6 +16,12 @@ afterEach(async () => { }); describe("/posts routes", () => { + + afterAll(async () => { + await emailQueue.close() + await imageProcessingQueue.close() + }) + test("GET /posts", async () => { const post = await Post.create({ title: "Post 1", diff --git a/__test__/workers/email.worker.test.ts b/__test__/workers/email.worker.test.ts deleted file mode 100644 index 46e9d9d..0000000 --- a/__test__/workers/email.worker.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { Worker } from "bullmq"; -import { emailQueue } from "../../src/queues"; -import connection from "../../src/queues/connection"; - -jest.mock("bullmq", () => ({ - ...jest.requireActual("bullmq"), - Worker: jest - .fn() - .mockImplementation((queueName, processingFunction, options) => { - const workerMock = { - on: jest.fn(), - }; - return workerMock; - }), -})); - -describe("email.worker", () => { - afterEach(() => { - jest.clearAllMocks(); - }); - - it("should create a new BullMQ worker for emailQueue", async () => { - require("../../src/workers/email.worker"); - expect(Worker).toHaveBeenCalledWith(emailQueue.name, expect.any(Function), { - connection, - }); - }); -}); diff --git a/package.json b/package.json index 5553f49..fb246e0 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "build": "npx tsc", "start": "node dist/index.js", "dev": "env-cmd nodemon ./src/index.ts", - "test": "env-cmd jest --coverage", + "test": "env-cmd jest --coverage --detectOpenHandles", "prettier": "npx prettier --write .", "lint": "npx eslint ." }, diff --git a/tsconfig.json b/tsconfig.json index 1865b5a..16d03ae 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,6 @@ "./src/**/*", "./__test__/**/*", ".eslintrc.json", - "jest.config.ts" + "jest.config.ts", ] }