-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37b78e2
commit d0516c9
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import _ from "lodash"; | ||
|
||
export class Env { | ||
private static environment: string | undefined = process.env.NODE_ENV; | ||
|
||
static instance(): Env { | ||
if (_.isUndefined(this.environment)) { | ||
this.environment = "development"; | ||
} | ||
|
||
return new Env(); | ||
} | ||
|
||
set(environment?: string): Env { | ||
Env.environment = environment; | ||
|
||
return Env.instance(); | ||
} | ||
|
||
get(): string { | ||
return Env.environment!; | ||
} | ||
|
||
is(environment: string): boolean { | ||
return this.get() === environment; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { Env } from "../src/env"; | ||
|
||
describe("env", () => { | ||
test("env correctly uses NODE_ENV", () => { | ||
const nodeEnv = process.env.NODE_ENV!; | ||
const env = Env.instance(); | ||
|
||
expect(env.get()).toBe(nodeEnv); | ||
expect(env.is(nodeEnv)).toBe(true); | ||
expect(env.is("staging")).toBe(false); | ||
}); | ||
|
||
test("env uses the fallback to NODE_ENV", () => { | ||
const env = Env.instance().set(undefined); | ||
|
||
expect(env.get()).toBe("development"); | ||
expect(env.is("development")).toBe(true); | ||
}); | ||
|
||
test("env uses the environment set to it", () => { | ||
const env = Env.instance().set("production"); | ||
|
||
expect(env.get()).toBe("production"); | ||
expect(env.is("production")).toBe(true); | ||
|
||
env.set("development"); | ||
expect(env.is("production")).toBe(false); | ||
}); | ||
}); |