|
| 1 | +// Updated Unit tests for: listen |
| 2 | + |
| 3 | +import express from "express"; |
| 4 | +import process from "process"; |
| 5 | +import { AppExpress } from "../application-express"; |
| 6 | + |
| 7 | +jest.mock("process", () => ({ |
| 8 | + ...jest.requireActual("process"), |
| 9 | + exit: jest.fn(), |
| 10 | + on: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock("../express-utils/inversify-express-server", () => { |
| 14 | + return { |
| 15 | + InversifyExpressServer: jest.fn().mockImplementation(() => ({ |
| 16 | + setConfig: jest.fn(), |
| 17 | + setErrorConfig: jest.fn(), |
| 18 | + build: jest.fn().mockReturnValue({ |
| 19 | + set: jest.fn(), |
| 20 | + listen: jest.fn().mockImplementation((port, callback) => { |
| 21 | + callback(); |
| 22 | + return { close: jest.fn() }; |
| 23 | + }), |
| 24 | + }), |
| 25 | + })), |
| 26 | + }; |
| 27 | +}); |
| 28 | + |
| 29 | +class MockLogger { |
| 30 | + error = jest.fn(); |
| 31 | + info = jest.fn(); |
| 32 | +} |
| 33 | + |
| 34 | +class MockConsole { |
| 35 | + messageServer = jest.fn(); |
| 36 | +} |
| 37 | + |
| 38 | +class MockAppContainer { |
| 39 | + Container = {}; |
| 40 | + create = jest.fn(); |
| 41 | +} |
| 42 | + |
| 43 | +class MockProviderManager {} |
| 44 | + |
| 45 | +class MockMiddleware { |
| 46 | + getMiddlewarePipeline = jest.fn().mockReturnValue([]); |
| 47 | + getErrorHandler = jest.fn(); |
| 48 | +} |
| 49 | + |
| 50 | +describe("AppExpress.listen() method", () => { |
| 51 | + let appExpress: AppExpress; |
| 52 | + let mockLogger: MockLogger; |
| 53 | + let mockConsole: MockConsole; |
| 54 | + let mockAppContainer: MockAppContainer; |
| 55 | + let mockProviderManager: MockProviderManager; |
| 56 | + let mockMiddlewareManager: MockMiddleware; |
| 57 | + let mockApp: express.Application; |
| 58 | + |
| 59 | + beforeEach(() => { |
| 60 | + mockLogger = new MockLogger(); |
| 61 | + mockConsole = new MockConsole(); |
| 62 | + mockAppContainer = new MockAppContainer(); |
| 63 | + mockProviderManager = new MockProviderManager(); |
| 64 | + mockMiddlewareManager = new MockMiddleware(); |
| 65 | + |
| 66 | + appExpress = new AppExpress(); |
| 67 | + |
| 68 | + // Replace the logger and console with mocks |
| 69 | + (appExpress as any).logger = mockLogger; |
| 70 | + (appExpress as any).console = mockConsole; |
| 71 | + (appExpress as any).appContainer = mockAppContainer; |
| 72 | + (appExpress as any).providerManager = mockProviderManager; |
| 73 | + (appExpress as any).middlewareManager = mockMiddlewareManager; |
| 74 | + |
| 75 | + // Mock the express application |
| 76 | + mockApp = { |
| 77 | + set: jest.fn(), |
| 78 | + listen: jest.fn().mockImplementation((port, callback) => { |
| 79 | + callback(); |
| 80 | + return { close: jest.fn() }; |
| 81 | + }), |
| 82 | + } as unknown as express.Application; |
| 83 | + |
| 84 | + // Mock the InversifyExpressServer |
| 85 | + const { InversifyExpressServer } = require("../express-utils/inversify-express-server"); |
| 86 | + InversifyExpressServer.mockImplementation(() => ({ |
| 87 | + setConfig: jest.fn(), |
| 88 | + setErrorConfig: jest.fn(), |
| 89 | + build: jest.fn().mockReturnValue(mockApp), |
| 90 | + })); |
| 91 | + }); |
| 92 | + |
| 93 | + describe("Happy paths", () => { |
| 94 | + it("should start the server on the given port", async () => { |
| 95 | + const port = 3000; |
| 96 | + await appExpress.listen(port); |
| 97 | + |
| 98 | + expect(mockApp.set).toHaveBeenCalledWith("env", "development"); |
| 99 | + expect(mockApp.listen).toHaveBeenCalledWith(port, expect.any(Function)); |
| 100 | + expect(mockConsole.messageServer).toHaveBeenCalledWith(port, "development", undefined); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should set the environment to development by default", async () => { |
| 104 | + await appExpress.listen(3000); |
| 105 | + |
| 106 | + expect(mockApp.set).toHaveBeenCalledWith("env", "development"); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe("Edge cases", () => { |
| 111 | + it("should handle string port by converting it to a number", async () => { |
| 112 | + const port = "3000"; |
| 113 | + await appExpress.listen(port); |
| 114 | + |
| 115 | + expect(mockApp.listen).toHaveBeenCalledWith(3000, expect.any(Function)); |
| 116 | + }); |
| 117 | + |
| 118 | + it("should handle invalid port by defaulting to 3000", async () => { |
| 119 | + await appExpress.listen(undefined as any); |
| 120 | + |
| 121 | + expect(mockApp.listen).toHaveBeenCalledWith(3000, expect.any(Function)); |
| 122 | + }); |
| 123 | + |
| 124 | + it("should handle process signals for graceful shutdown", async () => { |
| 125 | + await appExpress.listen(3000); |
| 126 | + |
| 127 | + const signals = ["SIGTERM", "SIGHUP", "SIGBREAK", "SIGQUIT", "SIGINT"]; |
| 128 | + signals.forEach((signal) => { |
| 129 | + expect(process.on).toHaveBeenCalledWith(signal, expect.any(Function)); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + it("should exit the process if no container is provided", async () => { |
| 134 | + (appExpress as any).appContainer = null; |
| 135 | + |
| 136 | + await expect(appExpress.listen(3000)).rejects.toThrow( |
| 137 | + "Cannot read properties of null (reading 'Container')", |
| 138 | + ); |
| 139 | + |
| 140 | + expect(mockLogger.error).toHaveBeenCalledWith( |
| 141 | + "No container provided for application configuration", |
| 142 | + "adapter-express", |
| 143 | + ); |
| 144 | + expect(process.exit).toHaveBeenCalledWith(1); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +// End of updated unit tests for: listen |
0 commit comments