Skip to content

Commit

Permalink
Add experimental tests for dealing with .env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
aedart committed May 2, 2024
1 parent 8292238 commit 325c505
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tests/browser/packages/xyz/env.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
describe('@aedart/xyz', () => {
describe('.env experiment', () => {

// This test is only able to pass if the "@import-meta-env/babel" babel plugin
// has been installed, and .env.example and .env files exists in the root of
// the project. DO NOT ENABLE THIS TEST IN CI!
xit('can read import.meta.env', () => {
// Debug
// console.log('ENV', import.meta.env.FOO);

const foo = import.meta.env.FOO;

expect(foo)
.toBe('BAR');
});

// @see https://import-meta-env.org/guide/getting-started/introduction.html#special-expression
xit('can iterate through properties in import.meta.env', () => {

console.log('import_meta_env', globalThis.import_meta_env);
console.log('FOO', import.meta.env.FOO);
console.log('Entries', Object.entries(import.meta.env));

const env = import.meta.env;

for (const property in env) {
console.log(`${property}: ${env[property]}`);
}
});

xit('can read and parse .env file', () => {

// globalThis.import_meta_env = JSON.parse('"import_meta_env_placeholder"');

console.log('FOO', import.meta.env.FOO);
console.log('import_meta_env', globalThis.import_meta_env);
});

// WINNER: This seems to be the most practical solution - or at least when using Webpack.
// Similar solutions SHOULD be available for babel, rollup, and plain typescript.
it('can read injected environment variables (via Webpack)', () => {
// Using the dotenv module and webpack, the contents if .env is injected into the _ENV
// variable.
// @see webpack.config.js
//console.log('ENV', _ENV);

const ENV = _ENV;

expect(ENV)
.withContext('_ENV has not been injected')
.not
.toBeUndefined();

expect(Reflect.has(ENV, 'FOO'))
.withContext('FOO does not exists in .env')
.toBeTrue();

expect(ENV.FOO)
.toBe('BAR');
});
});
});

0 comments on commit 325c505

Please sign in to comment.