Archived
1
0

refactor: use Teffen's solution for useEnv

This commit is contained in:
Joe Previte
2021-07-26 11:21:30 -07:00
parent 67e9eca942
commit 85d8c14b92
3 changed files with 64 additions and 33 deletions

View File

@ -1,5 +1,5 @@
import { promises as fs } from "fs"
import { tmpdir } from "../../test/utils/helpers"
import { tmpdir, useEnv } from "../../test/utils/helpers"
/**
* This file is for testing test helpers (not core code).
@ -12,3 +12,30 @@ describe("test helpers", () => {
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")
})
})