Skip to content

Commit 4ba0b89

Browse files
committed
chore: enhance coverage
1 parent 74de0f0 commit 4ba0b89

File tree

7 files changed

+534
-2
lines changed

7 files changed

+534
-2
lines changed

jest.config.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ const config: JestConfigWithTsJest = {
55
roots: ["<rootDir>/src"],
66
testRegex: ".*\\.spec\\.ts$",
77
testPathIgnorePatterns: ["/node_modules/", "/lib/"],
8-
collectCoverageFrom: ["src/**/*.ts", "!**/*.spec.ts", "src/**/index.ts", "!src/di/**"],
8+
collectCoverageFrom: [
9+
"src/**/*.ts",
10+
"!**/*.spec.ts",
11+
"!src/**/index.ts",
12+
"!src/di/**",
13+
"!src/adapter-express/express-utils/**",
14+
"!src/adapter-express/render/resolve-render.ts",
15+
"!src/adapter-express/micro-api/application-express-micro.ts",
16+
],
917
moduleNameMapper: {
1018
"^@src/(.*)$": "<rootDir>/src/$1",
1119
},

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@expressots/adapter-express",
3-
"version": "3.0.0-beta.2",
3+
"version": "3.0.0-beta.3",
44
"description": "Expressots - modern, fast, lightweight nodejs web framework (@adapter-express)",
55
"author": "",
66
"main": "./lib/cjs/index.js",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Unit tests for: close
2+
3+
import { AppExpress } from "../application-express";
4+
5+
// Mocking necessary dependencies
6+
class MockLogger {
7+
error = jest.fn();
8+
info = jest.fn();
9+
}
10+
11+
class MockConsole {
12+
messageServer = jest.fn();
13+
}
14+
15+
class MockHTTPServer {
16+
close = jest.fn((callback: (err?: Error) => void) => callback());
17+
}
18+
19+
jest.mock("express", () => {
20+
return {
21+
__esModule: true,
22+
default: jest.fn(() => ({
23+
use: jest.fn(),
24+
set: jest.fn(),
25+
listen: jest.fn((port: number, callback: () => void) => {
26+
callback();
27+
return new MockHTTPServer();
28+
}),
29+
})),
30+
};
31+
});
32+
33+
describe("AppExpress.close() close method", () => {
34+
let appExpress: AppExpress;
35+
let mockLogger: MockLogger;
36+
let mockConsole: MockConsole;
37+
let mockHTTPServer: MockHTTPServer;
38+
39+
beforeEach(() => {
40+
mockLogger = new MockLogger() as any;
41+
mockConsole = new MockConsole() as any;
42+
mockHTTPServer = new MockHTTPServer() as any;
43+
44+
appExpress = new AppExpress() as any;
45+
appExpress["logger"] = mockLogger as any;
46+
appExpress["console"] = mockConsole as any;
47+
appExpress["serverInstance"] = mockHTTPServer as any;
48+
});
49+
50+
describe("Happy paths", () => {
51+
it("should close the server successfully without logging", async () => {
52+
// Test to ensure the server closes without logging
53+
await expect(appExpress.close()).resolves.toBeUndefined();
54+
expect(mockHTTPServer.close).toHaveBeenCalled();
55+
expect(mockLogger.info).not.toHaveBeenCalled();
56+
});
57+
58+
it("should close the server successfully with logging", async () => {
59+
// Test to ensure the server closes with logging
60+
await expect(appExpress.close(true)).resolves.toBeUndefined();
61+
expect(mockHTTPServer.close).toHaveBeenCalled();
62+
expect(mockLogger.info).toHaveBeenCalledWith("Server closed successfully", "adapter-express");
63+
});
64+
});
65+
66+
describe("Edge cases", () => {
67+
it("should handle error during server close without logging", async () => {
68+
// Test to ensure error handling during server close without logging
69+
const error = new Error("Close error");
70+
mockHTTPServer.close = jest.fn((callback: (err?: Error) => void) => callback(error));
71+
72+
await expect(appExpress.close()).rejects.toThrow("Close error");
73+
expect(mockLogger.error).not.toHaveBeenCalled();
74+
});
75+
76+
it("should handle error during server close with logging", async () => {
77+
// Test to ensure error handling during server close with logging
78+
const error = new Error("Close error");
79+
mockHTTPServer.close = jest.fn((callback: (err?: Error) => void) => callback(error));
80+
81+
await expect(appExpress.close(true)).rejects.toThrow("Close error");
82+
expect(mockLogger.error).toHaveBeenCalledWith(
83+
"Error closing server: Close error",
84+
"adapter-express",
85+
);
86+
});
87+
88+
it("should resolve immediately if serverInstance is null", async () => {
89+
// Test to ensure immediate resolution if serverInstance is null
90+
appExpress["serverInstance"] = null;
91+
await expect(appExpress.close()).resolves.toBeUndefined();
92+
});
93+
});
94+
});
95+
96+
// End of unit tests for: close
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Unit tests for: getHttpServer
2+
3+
import express from "express";
4+
import { AppExpress } from "../application-express";
5+
6+
// Mocking necessary functions and classes
7+
jest.mock("../render/engine", () => {
8+
const actual = jest.requireActual("../render/engine");
9+
return {
10+
...actual,
11+
setEngineEjs: jest.fn(),
12+
setEngineHandlebars: jest.fn(),
13+
setEnginePug: jest.fn(),
14+
};
15+
});
16+
17+
class MockLogger {
18+
error = jest.fn();
19+
}
20+
21+
class MockConsole {
22+
messageServer = jest.fn();
23+
}
24+
25+
class MockAppContainer {
26+
Container = {};
27+
}
28+
29+
class MockProviderManager {}
30+
31+
describe("AppExpress.getHttpServer() getHttpServer method", () => {
32+
let appExpress: AppExpress;
33+
34+
beforeEach(() => {
35+
appExpress = new AppExpress() as any;
36+
appExpress["logger"] = new MockLogger() as any;
37+
appExpress["console"] = new MockConsole() as any;
38+
appExpress["appContainer"] = new MockAppContainer() as any;
39+
appExpress["middlewareManager"] = { getErrorHandler: jest.fn() } as any;
40+
appExpress["providerManager"] = new MockProviderManager() as any;
41+
appExpress["app"] = express() as any;
42+
});
43+
44+
describe("Happy Paths", () => {
45+
it("should return the express application instance when app is initialized", async () => {
46+
// Test to ensure the method returns the express app instance when initialized
47+
const appInstance = await appExpress.getHttpServer();
48+
expect(appInstance).toBe(appExpress["app"]);
49+
});
50+
});
51+
52+
describe("Edge Cases", () => {
53+
it("should throw an error if the app is not initialized", async () => {
54+
// Test to ensure an error is thrown when the app is not initialized
55+
appExpress["app"] = null as any;
56+
await expect(appExpress.getHttpServer()).rejects.toThrow(
57+
"Incorrect usage of `getHttpServer` method",
58+
);
59+
});
60+
});
61+
});
62+
63+
// End of unit tests for: getHttpServer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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

Comments
 (0)