Skip to content

Commit 85d8c14

Browse files
committed
refactor: use Teffen's solution for useEnv
1 parent 67e9eca commit 85d8c14

File tree

3 files changed

+64
-33
lines changed

3 files changed

+64
-33
lines changed

test/unit/helpers.test.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { promises as fs } from "fs"
2-
import { tmpdir } from "../../test/utils/helpers"
2+
import { tmpdir, useEnv } from "../../test/utils/helpers"
33

44
/**
55
* This file is for testing test helpers (not core code).
@@ -12,3 +12,30 @@ describe("test helpers", () => {
1212
expect(fs.access(pathToTempDir)).resolves.toStrictEqual(undefined)
1313
})
1414
})
15+
16+
describe("useEnv", () => {
17+
beforeAll(() => {
18+
jest.resetModules()
19+
process.env.TEST_USE_ENV = "test environment variable"
20+
})
21+
afterAll(() => {
22+
delete process.env.TEST_USE_ENV
23+
})
24+
it("should set and reset the env var", () => {
25+
const envKey = "TEST_ENV_VAR"
26+
const [setValue, resetValue] = useEnv(envKey)
27+
setValue("hello-world")
28+
expect(process.env[envKey]).toEqual("hello-world")
29+
resetValue()
30+
expect(process.env[envKey]).toEqual(undefined)
31+
})
32+
it("should set and reset the env var where a value was already set", () => {
33+
const envKey = "TEST_USE_ENV"
34+
expect(process.env[envKey]).toEqual("test environment variable")
35+
const [setValue, resetValue] = useEnv(envKey)
36+
setValue("hello there")
37+
expect(process.env[envKey]).toEqual("hello there")
38+
resetValue()
39+
expect(process.env[envKey]).toEqual("test environment variable")
40+
})
41+
})

test/unit/node/proxy_agent.test.ts

+12-32
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,39 @@
11
import { shouldEnableProxy } from "../../../src/node/proxy_agent"
2-
3-
/**
4-
* Helper function to set an env variable.
5-
*
6-
* Returns a function to cleanup the env variable.
7-
*/
8-
function setEnv(name: string, value: string) {
9-
process.env[name] = value
10-
11-
return {
12-
cleanup() {
13-
delete process.env[name]
14-
},
15-
}
16-
}
2+
import { useEnv } from "../../utils/helpers"
173

184
describe("shouldEnableProxy", () => {
19-
// Source: https://stackoverflow.com/a/48042799
20-
const OLD_ENV = process.env
5+
const [setHTTPProxy, resetHTTPProxy] = useEnv("HTTP_PROXY")
6+
const [setHTTPSProxy, resetHTTPSProxy] = useEnv("HTTPS_PROXY")
7+
const [setNoProxy, resetNoProxy] = useEnv("NO_PROXY")
218

229
beforeEach(() => {
2310
jest.resetModules() // Most important - it clears the cache
24-
process.env = { ...OLD_ENV } // Make a copy
25-
})
26-
27-
afterAll(() => {
28-
process.env = OLD_ENV // Restore old environment
11+
resetHTTPProxy()
12+
resetNoProxy()
13+
resetHTTPSProxy()
2914
})
3015

3116
it("returns true when HTTP_PROXY is set", () => {
32-
const { cleanup } = setEnv("HTTP_PROXY", "http://proxy.example.com")
17+
setHTTPProxy("http://proxy.example.com")
3318
expect(shouldEnableProxy()).toBe(true)
34-
cleanup()
3519
})
3620
it("returns true when HTTPS_PROXY is set", () => {
37-
const { cleanup } = setEnv("HTTPS_PROXY", "http://proxy.example.com")
21+
setHTTPSProxy("https://proxy.example.com")
3822
expect(shouldEnableProxy()).toBe(true)
39-
cleanup()
4023
})
4124
it("returns false when NO_PROXY is set", () => {
42-
const { cleanup } = setEnv("NO_PROXY", "*")
25+
setNoProxy("*")
4326
expect(shouldEnableProxy()).toBe(false)
44-
cleanup()
4527
})
4628
it("should return false when neither HTTP_PROXY nor HTTPS_PROXY is set", () => {
4729
expect(shouldEnableProxy()).toBe(false)
4830
})
4931
it("should return false when NO_PROXY is set to https://example.com", () => {
50-
const { cleanup } = setEnv("NO_PROXY", "https://example.com")
32+
setNoProxy("https://example.com")
5133
expect(shouldEnableProxy()).toBe(false)
52-
cleanup()
5334
})
5435
it("should return false when NO_PROXY is set to http://example.com", () => {
55-
const { cleanup } = setEnv("NO_PROXY", "http://example.com")
36+
setNoProxy("http://example.com")
5637
expect(shouldEnableProxy()).toBe(false)
57-
cleanup()
5838
})
5939
})

test/utils/helpers.ts

+24
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,27 @@ export async function tmpdir(testName: string): Promise<string> {
3737

3838
return await fs.mkdtemp(path.join(dir, `${testName}-`), { encoding: "utf8" })
3939
}
40+
41+
/**
42+
* @description Helper function to use an environment variable.
43+
*
44+
* @returns an array (similar to useState in React) with a function
45+
* to set the value and reset the value
46+
*/
47+
export function useEnv(key: string): [(nextValue: string | undefined) => string | undefined, () => void] {
48+
const initialValue = process.env[key]
49+
const setValue = (nextValue: string | undefined) => (process.env[key] = nextValue)
50+
// Node automatically converts undefined to string 'undefined'
51+
// when assigning an environment variable.
52+
// which is why we need to delete it if it's supposed to be undefined
53+
// Source: https://stackoverflow.com/a/60147167
54+
const resetValue = () => {
55+
if (initialValue !== undefined) {
56+
process.env[key] = initialValue
57+
} else {
58+
delete process.env[key]
59+
}
60+
}
61+
62+
return [setValue, resetValue]
63+
}

0 commit comments

Comments
 (0)