Repository navigation
Expand file tree
/
Copy pathdotenv.test.js
More file actions
105 lines (88 loc) · 2.69 KB
/
Copy pathdotenv.test.js
File metadata and controls
105 lines (88 loc) · 2.69 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Port of src/test/java/tests/DotenvTests.java
*/
import "./helpers.js";
import assert from "node:assert/strict";
import test, { beforeEach } from "node:test";
import {
Dotenv,
DotenvEntry,
DotenvException,
UnsupportedOperationError,
} from "../src/index.js";
/** @type {Map<string, string>} */
let envVars;
beforeEach(() => {
envVars = new Map([
["MY_TEST_EV1", "my test ev 1"],
["MY_TEST_EV2", "my test ev 2"],
["WITHOUT_VALUE", ""],
]);
});
test("throwIfMalconfigured", () => {
assert.throws(() => Dotenv.configure().load(), DotenvException);
});
test("load", () => {
assert.throws(() => {
const dotenv = Dotenv.load();
envVars.forEach((expected, key) =>
assert.strictEqual(dotenv.get(key), expected)
);
}, DotenvException);
});
test("iteratorOverDotenv", () => {
const dotenv = Dotenv.configure().ignoreIfMalformed().load();
dotenv
.entries()
.forEach((e) => assert.strictEqual(e.getValue(), dotenv.get(e.getKey())));
for (const e of dotenv.entries()) {
assert.strictEqual(e.getValue(), dotenv.get(e.getKey()));
}
});
test("iteratorOverDotenvWithFilter", () => {
const dotenv = Dotenv.configure().ignoreIfMalformed().load();
const entriesInFile = dotenv.entries(Dotenv.Filter.DECLARED_IN_ENV_FILE);
const entriesAll = dotenv.entries();
assert.ok(entriesInFile.size < entriesAll.size);
for (const [key, value] of envVars) {
assert.strictEqual(value, dotenv.get(key));
}
});
test("failToRemoveFromDotenv", () => {
const dotenv = Dotenv.configure().ignoreIfMalformed().load();
const entries = dotenv.entries();
// The JVM test removes through the iterator; a JS Set is mutated directly.
assert.throws(() => {
for (const e of entries) entries.delete(e);
}, UnsupportedOperationError);
});
test("failToAddToDotenv", () => {
const dotenv = Dotenv.configure().ignoreIfMalformed().load();
const entries = dotenv.entries();
assert.throws(
() => entries.add(new DotenvEntry("new", "value")),
UnsupportedOperationError
);
});
test("configureWithIgnoreMalformed", () => {
const dotenv = Dotenv.configure().ignoreIfMalformed().load();
for (const [key, expected] of envVars) {
assert.strictEqual(dotenv.get(key), expected);
}
});
test("configureWithIgnoreMissingAndMalformed", () => {
const dotenv = Dotenv.configure()
.directory("/missing/dir")
.ignoreIfMalformed()
.ignoreIfMissing()
.load();
assert.notStrictEqual(dotenv.get("PATH"), null);
});
test("malformedWithUncloseQuote", () => {
const dotenv = Dotenv.configure()
.directory("/unclosed.quote")
.ignoreIfMalformed()
.load();
assert.strictEqual(dotenv.get("FOO"), null);
assert.strictEqual(dotenv.get("BAZ"), null);
});