-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathhelpers.test.ts
58 lines (54 loc) · 1.81 KB
/
helpers.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { promises as fs } from "fs"
import { clean, getAvailablePort, tmpdir, useEnv } from "../../test/utils/helpers"
/**
* This file is for testing test helpers (not core code).
*/
describe("test helpers", () => {
const testName = "temp-dir"
beforeAll(async () => {
await clean(testName)
})
it("should return a temp directory", async () => {
const pathToTempDir = await tmpdir(testName)
expect(pathToTempDir).toContain(testName)
expect(fs.access(pathToTempDir)).resolves.toStrictEqual(undefined)
})
})
describe("useEnv", () => {
beforeAll(() => {
jest.resetModules()
process.env.TEST_USE_ENV = "test environment variable"
})
afterAll(() => {
delete process.env.TEST_USE_ENV
})
it("should set and reset the env var", () => {
const envKey = "TEST_ENV_VAR"
const [setValue, resetValue] = useEnv(envKey)
setValue("hello-world")
expect(process.env[envKey]).toEqual("hello-world")
resetValue()
expect(process.env[envKey]).toEqual(undefined)
})
it("should set and reset the env var where a value was already set", () => {
const envKey = "TEST_USE_ENV"
expect(process.env[envKey]).toEqual("test environment variable")
const [setValue, resetValue] = useEnv(envKey)
setValue("hello there")
expect(process.env[envKey]).toEqual("hello there")
resetValue()
expect(process.env[envKey]).toEqual("test environment variable")
})
})
describe("getAvailablePort", () => {
it("should return a valid port", async () => {
const port = await getAvailablePort()
expect(port).toBeGreaterThan(0)
expect(port).toBeLessThanOrEqual(65535)
})
it("should return different ports for different calls", async () => {
const portOne = await getAvailablePort()
const portTwo = await getAvailablePort()
expect(portOne).not.toEqual(portTwo)
})
})