-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from sophiabrandt/env-var-wrapper
Env var wrapper
- Loading branch information
Showing
4 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export const viteEnvVar = Object.freeze({ | ||
cache: new Map<string, string>(), | ||
|
||
get(name: string) { | ||
if (this.cache.has(name)) { | ||
return this.cache.get(name); | ||
} | ||
const value = import.meta.env[name]; | ||
this.assertNoUndefined(value, name); | ||
|
||
this.cache.set(name, value); | ||
|
||
return value; | ||
}, | ||
|
||
assertNoUndefined(value: string, name: string) { | ||
if (value === undefined) { | ||
throw new Error(`Environment variable ${name} not found`); | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { viteEnvVar } from '@/utils/vite-env-var'; | ||
|
||
describe('viteEnvVar', () => { | ||
const originalEnv = { ...import.meta.env }; | ||
|
||
beforeEach(() => { | ||
const mockEnv = { ...originalEnv }; | ||
Object.defineProperty(import.meta, 'env', { | ||
value: mockEnv, | ||
writable: true, | ||
configurable: true, | ||
}); | ||
// Clear cache before each test | ||
viteEnvVar.cache.clear(); | ||
}); | ||
|
||
afterEach(() => { | ||
Object.defineProperty(import.meta, 'env', { | ||
value: originalEnv, | ||
writable: false, | ||
configurable: true, | ||
}); | ||
}); | ||
|
||
it('returns the value of an environment variable', () => { | ||
const expected = 'test'; | ||
import.meta.env.NODE_ENV = expected; | ||
|
||
const actual = viteEnvVar.get('NODE_ENV'); | ||
|
||
expect(actual).toBe(expected); | ||
}); | ||
|
||
it('caches the value of an environment variable', () => { | ||
// Arrange | ||
const expected = 'test'; | ||
import.meta.env.NODE_ENV = expected; | ||
const getFromCacheSpy = vi.spyOn(viteEnvVar.cache, 'get'); | ||
const setToCacheSpy = vi.spyOn(viteEnvVar.cache, 'set'); | ||
|
||
// Act | ||
viteEnvVar.get('NODE_ENV'); | ||
viteEnvVar.get('NODE_ENV'); | ||
|
||
// Assert | ||
expect(getFromCacheSpy).toHaveBeenCalledWith('NODE_ENV'); | ||
expect(setToCacheSpy).toHaveBeenCalledWith('NODE_ENV', expected); | ||
expect(getFromCacheSpy).toHaveBeenCalledTimes(1); | ||
expect(setToCacheSpy).toHaveBeenCalledTimes(1); | ||
expect(viteEnvVar.cache.size).toBe(1); | ||
expect(viteEnvVar.get('NODE_ENV')).toBe(expected); | ||
}); | ||
|
||
it('throws an error if value cannot be found', () => { | ||
expect(() => viteEnvVar.get('NON_EXISTENT_VAR')).toThrow( | ||
'Environment variable NON_EXISTENT_VAR not found' | ||
); | ||
}); | ||
}); |