Skip to content

Commit c3553ba

Browse files
committed
tests(src/utils/config): add test for injectEnv
1 parent df24dff commit c3553ba

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

src/utils/__tests__/config.test.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { injectEnv } from "../config"
2+
3+
describe("injectEnv", () => {
4+
const originalEnv = process.env
5+
6+
beforeEach(() => {
7+
// Assign a new / reset process.env before each test
8+
jest.resetModules()
9+
process.env = { ...originalEnv }
10+
})
11+
12+
afterAll(() => {
13+
// Restore original process.env after all tests
14+
process.env = originalEnv
15+
})
16+
17+
it("should replace env variables in a string", async () => {
18+
process.env.TEST_VAR = "testValue"
19+
const configString = "Hello ${env:TEST_VAR}"
20+
const expectedString = "Hello testValue"
21+
const result = await injectEnv(configString)
22+
expect(result).toBe(expectedString)
23+
})
24+
25+
it("should replace env variables in an object", async () => {
26+
process.env.API_KEY = "12345"
27+
process.env.ENDPOINT = "https://example.com"
28+
const configObject = {
29+
key: "${env:API_KEY}",
30+
url: "${env:ENDPOINT}",
31+
nested: {
32+
value: "Keep this ${env:API_KEY}",
33+
},
34+
}
35+
const expectedObject = {
36+
key: "12345",
37+
url: "https://example.com",
38+
nested: {
39+
value: "Keep this 12345",
40+
},
41+
}
42+
const result = await injectEnv(configObject)
43+
expect(result).toEqual(expectedObject)
44+
})
45+
46+
it("should use notFoundValue for missing env variables", async () => {
47+
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation()
48+
process.env.EXISTING_VAR = "exists"
49+
const configString = "Value: ${env:EXISTING_VAR}, Missing: ${env:MISSING_VAR}"
50+
const expectedString = "Value: exists, Missing: NOT_FOUND"
51+
const result = await injectEnv(configString, "NOT_FOUND")
52+
expect(result).toBe(expectedString)
53+
expect(consoleWarnSpy).toHaveBeenCalledWith(
54+
"[injectEnv] env variable MISSING_VAR referenced but not found in process.env",
55+
)
56+
consoleWarnSpy.mockRestore()
57+
})
58+
59+
it("should use default empty string for missing env variables if notFoundValue is not provided", async () => {
60+
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation()
61+
const configString = "Missing: ${env:ANOTHER_MISSING}"
62+
const expectedString = "Missing: "
63+
const result = await injectEnv(configString)
64+
expect(result).toBe(expectedString)
65+
expect(consoleWarnSpy).toHaveBeenCalledWith(
66+
"[injectEnv] env variable ANOTHER_MISSING referenced but not found in process.env",
67+
)
68+
consoleWarnSpy.mockRestore()
69+
})
70+
71+
it("should handle strings without env variables", async () => {
72+
const configString = "Just a regular string"
73+
const result = await injectEnv(configString)
74+
expect(result).toBe(configString)
75+
})
76+
77+
it("should handle objects without env variables", async () => {
78+
const configObject = { key: "value", number: 123 }
79+
const result = await injectEnv(configObject)
80+
expect(result).toEqual(configObject)
81+
})
82+
83+
it("should not mutate the original object", async () => {
84+
process.env.MUTATE_TEST = "mutated"
85+
const originalObject = { value: "${env:MUTATE_TEST}" }
86+
const copyOfOriginal = { ...originalObject } // Shallow copy for comparison
87+
await injectEnv(originalObject)
88+
expect(originalObject).toEqual(copyOfOriginal) // Check if the original object remains unchanged
89+
})
90+
91+
it("should handle empty string input", async () => {
92+
const result = await injectEnv("")
93+
expect(result).toBe("")
94+
})
95+
96+
it("should handle empty object input", async () => {
97+
const result = await injectEnv({})
98+
expect(result).toEqual({})
99+
})
100+
101+
it("should handle env variables with dots in the name", async () => {
102+
process.env["TEST.VAR.WITH.DOTS"] = "dottedValue"
103+
const configString = "Value: ${env:TEST.VAR.WITH.DOTS}"
104+
const expectedString = "Value: dottedValue"
105+
const result = await injectEnv(configString)
106+
expect(result).toBe(expectedString)
107+
})
108+
})

0 commit comments

Comments
 (0)