-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
89 lines (74 loc) · 2.38 KB
/
index.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import test from "ava";
const importFile = async () => await import("./index.js");
test("get env that was originally set on process.env", async (t) => {
process.env.hello = "world";
const senvf = await importFile();
t.is(senvf.default.has("hello"), true);
t.is(senvf.default.get("hello"), "world");
});
test("can't get process.env that was originally set on process.env", async (t) => {
process.env.hello = "world";
await importFile();
t.is(typeof process.env.hello, "undefined");
});
test("can't set process.env", async (t) => {
await importFile();
t.is(typeof process.env.hello, "undefined");
process.env.hello = "world";
t.is(typeof process.env.hello, "undefined");
});
test("setting process.env sets senvf", async (t) => {
const senvf = await importFile();
process.env.foo = "bar";
t.is(senvf.default.get("foo"), "bar");
});
test("get returns fallback if property not set", async (t) => {
const senvf = await importFile();
t.is(senvf.default.get("a", "b"), "b");
});
test("has returns false if property not set", async (t) => {
const senvf = await importFile();
t.is(senvf.default.has("ab"), false);
});
// even those these tests are not type safe, this package can be used
// outside of TS, so we still need to test
test("can be set to number", async (t) => {
const senvf = await importFile();
// @ts-ignore
process.env.foo = 1;
t.is(senvf.default.get("foo"), 1);
});
test("can be set to object", async (t) => {
const senvf = await importFile();
// @ts-ignore
process.env.foo = { a: "b" };
t.deepEqual(senvf.default.get("foo"), { a: "b" });
});
test("can be set to array", async (t) => {
const senvf = await importFile();
// @ts-ignore
process.env.foo = ["a", "b"];
t.deepEqual(senvf.default.get("foo"), ["a", "b"]);
});
test("can be set to null", async (t) => {
const senvf = await importFile();
// @ts-ignore
process.env.foo = null;
t.is(senvf.default.get("foo"), null);
});
test("can be set to function", async (t) => {
const senvf = await importFile();
// @ts-ignore
process.env.foo = () => {};
t.is(typeof senvf.default.get("foo"), "function");
});
test("cannot get properties directly", async (t) => {
const senvf = await importFile();
// @ts-ignore
t.is(typeof senvf.a, "undefined");
});
test("cannot set properties directly", async (t) => {
const senvf = await importFile();
// @ts-ignore
t.throws(() => (senvf.b = "b"));
});