Skip to content

Commit

Permalink
Add tests for loading environment variables from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
aedart committed Nov 10, 2024
1 parent 143d360 commit 6a57cf0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/cli/packages/cli/env.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, it, beforeEach } from 'node:test';
import * as assert from "node:assert";
import makeCliApplication from "./helpers/makeCliApplication.js";
import { Env } from "@aedart/support/env";

describe('@aedart/cli', () => {

describe('env', () => {

beforeEach(() => {
Env.clear();
});

it('loads default .env file', async () => {
const cli = makeCliApplication({
writeOut: () => {
// Ignore output
}
});
const core = cli.core;

let hasLoadedEnv = false;
core.destroying(() => {
hasLoadedEnv = Env.get('APP_ENV') === 'testing';
});

// --------------------------------------------------------------- //

await cli.run([], { from: 'user' });

assert.ok(hasLoadedEnv, 'environment file not loaded');
});

it('can load custom environment file', async () => {
const cli = makeCliApplication({
writeOut: () => {
// Ignore output
}
});
const core = cli.core;

let hasLoadedEnv = false;
core.destroying(() => {
hasLoadedEnv = Env.get('CUSTOM_KEY') === 'custom';
});

// --------------------------------------------------------------- //

await cli.run([
'--env=tests/cli/packages/cli/fixtures/.custom'
], { from: 'user' });

assert.ok(hasLoadedEnv, 'custom environment file not loaded');
});
});
});
8 changes: 8 additions & 0 deletions tests/cli/packages/cli/fixtures/.custom
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Custom environment file

# Application Environment
# NOTE: Key might NOT be set, if previously defined in `process.env`!
APP_ENV=custom

# A custom key...
CUSTOM_KEY=custom

0 comments on commit 6a57cf0

Please sign in to comment.