Skip to content

Commit 6a57cf0

Browse files
committed
Add tests for loading environment variables from a file
1 parent 143d360 commit 6a57cf0

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

tests/cli/packages/cli/env.test.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, it, beforeEach } from 'node:test';
2+
import * as assert from "node:assert";
3+
import makeCliApplication from "./helpers/makeCliApplication.js";
4+
import { Env } from "@aedart/support/env";
5+
6+
describe('@aedart/cli', () => {
7+
8+
describe('env', () => {
9+
10+
beforeEach(() => {
11+
Env.clear();
12+
});
13+
14+
it('loads default .env file', async () => {
15+
const cli = makeCliApplication({
16+
writeOut: () => {
17+
// Ignore output
18+
}
19+
});
20+
const core = cli.core;
21+
22+
let hasLoadedEnv = false;
23+
core.destroying(() => {
24+
hasLoadedEnv = Env.get('APP_ENV') === 'testing';
25+
});
26+
27+
// --------------------------------------------------------------- //
28+
29+
await cli.run([], { from: 'user' });
30+
31+
assert.ok(hasLoadedEnv, 'environment file not loaded');
32+
});
33+
34+
it('can load custom environment file', async () => {
35+
const cli = makeCliApplication({
36+
writeOut: () => {
37+
// Ignore output
38+
}
39+
});
40+
const core = cli.core;
41+
42+
let hasLoadedEnv = false;
43+
core.destroying(() => {
44+
hasLoadedEnv = Env.get('CUSTOM_KEY') === 'custom';
45+
});
46+
47+
// --------------------------------------------------------------- //
48+
49+
await cli.run([
50+
'--env=tests/cli/packages/cli/fixtures/.custom'
51+
], { from: 'user' });
52+
53+
assert.ok(hasLoadedEnv, 'custom environment file not loaded');
54+
});
55+
});
56+
});
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Custom environment file
2+
3+
# Application Environment
4+
# NOTE: Key might NOT be set, if previously defined in `process.env`!
5+
APP_ENV=custom
6+
7+
# A custom key...
8+
CUSTOM_KEY=custom

0 commit comments

Comments
 (0)