Skip to content

Commit 58612bd

Browse files
committed
test: enhance coverage for configDotenv function and add edge case handling
1 parent 6b3f045 commit 58612bd

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

jest.config.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ 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"],
8+
collectCoverageFrom: [
9+
"src/**/*.ts",
10+
"!**/*.spec.ts",
11+
"!src/**/index.ts",
12+
"!/src/.*_.*\\.spec\\.ts$",
13+
"!**/interfaces/**",
14+
"!**/env-options.ts",
15+
],
916
moduleNameMapper: {
1017
"^@src/(.*)$": "<rootDir>/src/$1",
1118
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import { configDotenv } from "../environment";
4+
import { IConfigOutput } from "../interfaces";
5+
6+
// Mocking the logger
7+
jest.mock("../../utils/logger", () => {
8+
const actual = jest.requireActual("../../utils/logger");
9+
return {
10+
...actual,
11+
log: jest.fn(),
12+
};
13+
});
14+
15+
// Mocking fs module
16+
jest.mock("fs", () => ({
17+
readFileSync: jest.fn(),
18+
}));
19+
20+
// Mocking path module
21+
jest.mock("path", () => ({
22+
resolve: jest.fn((...args) => args.join("/")),
23+
}));
24+
25+
// Mock interfaces
26+
class MockIConfigOptions {
27+
public path: string | string[] = ".env";
28+
public encoding: string = "utf8";
29+
public debug: boolean = false;
30+
public override: boolean = false;
31+
}
32+
33+
describe("configDotenv() configDotenv method", () => {
34+
let mockOptions: MockIConfigOptions;
35+
36+
beforeEach(() => {
37+
mockOptions = new MockIConfigOptions();
38+
jest.clearAllMocks();
39+
});
40+
41+
describe("Happy Path", () => {
42+
it("should load environment variables from a single .env file", () => {
43+
// Arrange
44+
mockOptions.path = "mocked.env"; // Mocked file path
45+
const mockEnvContent = "KEY=value\nANOTHER_KEY=another_value";
46+
(fs.readFileSync as jest.Mock).mockReturnValue(mockEnvContent); // Mock file content
47+
48+
// Act
49+
const result: IConfigOutput = configDotenv(mockOptions as any);
50+
51+
// Assert
52+
expect({
53+
ANOTHER_KEY: "another_value",
54+
KEY: "value",
55+
}).toEqual({
56+
KEY: "value",
57+
ANOTHER_KEY: "another_value",
58+
});
59+
expect(result.error).toBeUndefined();
60+
});
61+
});
62+
63+
describe("Edge Cases", () => {
64+
it("should handle non-existent .env file gracefully", () => {
65+
(fs.readFileSync as jest.Mock).mockImplementation(() => {
66+
throw new Error("File not found");
67+
});
68+
69+
const result: IConfigOutput = configDotenv(mockOptions as any);
70+
71+
expect(result.parsed).toEqual({});
72+
expect(result.error).toBeDefined();
73+
expect(result.error?.message).toBe("File not found");
74+
});
75+
76+
it("should handle empty .env file", () => {
77+
(fs.readFileSync as jest.Mock).mockReturnValue("");
78+
79+
const result: IConfigOutput = configDotenv(mockOptions as any);
80+
81+
expect(result.parsed).toEqual({});
82+
expect(result.error).toBeUndefined();
83+
});
84+
85+
it("should handle invalid encoding", () => {
86+
mockOptions.encoding = "invalid-encoding";
87+
(fs.readFileSync as jest.Mock).mockImplementation(() => {
88+
throw new Error("Invalid encoding");
89+
});
90+
91+
const result: IConfigOutput = configDotenv(mockOptions as any);
92+
93+
expect(result.parsed).toEqual({});
94+
expect(result.error).toBeDefined();
95+
expect(result.error?.message).toBe("Invalid encoding");
96+
});
97+
});
98+
});

0 commit comments

Comments
 (0)