Skip to content

Instantly share code, notes, and snippets.

@nebez
Created September 17, 2018 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nebez/d411e6122358b4ac305f0b55afdc3a74 to your computer and use it in GitHub Desktop.
Save nebez/d411e6122358b4ac305f0b55afdc3a74 to your computer and use it in GitHub Desktop.
Environment variable loading tests
import { env, MissingEnvironmentVariableError, InvalidEnvironmentVariableError } from 'src/Application/env';
describe('Environment Variable Loader', () => {
const originalProcessEnv = Object.assign({}, process.env);
afterEach(() => {
process.env = originalProcessEnv;
});
describe('Default values with non-existent env vars', () => {
it('should accept number defaults', () => {
expect(env('NON_EXISTENT', 99)).toEqual(99);
});
it('should accept string defaults', () => {
expect(env('NON_EXISTENT', 'val')).toEqual('val');
});
it('should accept boolean defaults', () => {
expect(env('NON_EXISTENT', false)).toEqual(false);
});
it('should not throw when null default value is passed', () => {
expect(env('NON_EXISTENT', null)).toEqual(null);
});
});
describe('Value transformers with defaults as primitives', () => {
it('should transform to number when default is number and value is present', () => {
process.env.RANDOM_VAL = '12345';
expect(env('RANDOM_VAL', 99)).toEqual(12345);
process.env.RANDOM_VAL = '12345.6';
expect(env('RANDOM_VAL', 99)).toEqual(12345.6);
});
it('should transform to string when default is string and value is present', () => {
process.env.RANDOM_VAL = '12345';
expect(env('RANDOM_VAL', '99')).toEqual('12345');
});
it('should transform to boolean when default is boolean and value is present', () => {
process.env.RANDOM_VAL = 'true';
expect(env('RANDOM_VAL', false)).toEqual(true);
process.env.RANDOM_VAL = 'false';
expect(env('RANDOM_VAL', false)).toEqual(false);
process.env.RANDOM_VAL = '1';
expect(env('RANDOM_VAL', false)).toEqual(true);
process.env.RANDOM_VAL = '0';
expect(env('RANDOM_VAL', false)).toEqual(false);
});
it('should throw when number default and invalid value is present', () => {
process.env.RANDOM_VAL = 'abc';
expect(() => env('RANDOM_VAL', 99)).toThrowError(InvalidEnvironmentVariableError);
process.env.RANDOM_VAL = '1234abc';
expect(() => env('RANDOM_VAL', 99)).toThrowError(InvalidEnvironmentVariableError);
});
it('should throw when boolean default and invalid value is present', () => {
process.env.RANDOM_VAL = 'troo';
expect(() => env('RANDOM_VAL', false)).toThrowError(InvalidEnvironmentVariableError);
});
});
describe('No default values', () => {
it('should throw when value does not exist', () => {
expect(() => env('NON_EXISTENT')).toThrowError(MissingEnvironmentVariableError);
})
it('should not throw when value does exist', () => {
process.env.RANDOM_VAL = 'no throw!';
expect(env('RANDOM_VAL')).toEqual('no throw!');
});
});
describe('Value transformers', () => {
const nodeEnvParser = (input: string) => {
if (input === 'production' || input === 'development') {
return input;
}
throw new InvalidEnvironmentVariableError(`Invalid NODE_ENV value: ${input}`);
}
it('should transform input to and narrow type', () => {
process.env.FAKE_NODE_ENV = 'development';
// The explicit type declaration here for the variable fakeNodeEnv
// serves as a test of the typescript compiler and the type
// narrowing. If you change the explicit declaration or the return
// type of nodeEnvParser(), this should raise a type error.
const fakeNodeEnv: 'development' | 'production' = env('FAKE_NODE_ENV', nodeEnvParser);
expect(fakeNodeEnv).toEqual('development');
});
it('should throw error if input is wrong type', () => {
process.env.FAKE_NODE_ENV = 'staging';
expect(() => env('FAKE_NODE_ENV', nodeEnvParser)).toThrowError(InvalidEnvironmentVariableError);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment