Skip to content

Commit

Permalink
Merge pull request #12 from sophiabrandt/env-var-wrapper
Browse files Browse the repository at this point in the history
Env var wrapper
  • Loading branch information
sophiabrandt authored Jun 30, 2024
2 parents 8b68233 + 37c3435 commit f422cf4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
- uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- name: Set environment for Test
run: echo "VITE_API_KEY=xxxxx" > .env
- name: Install Dependencies
run: bun install --frozen-lockfile
- name: Unit Tests
Expand Down
4 changes: 3 additions & 1 deletion src/restaurants/RestaurantContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { createContext, useMemo } from 'react';
import { RestaurantStore } from './store/RestaurantStore';
import { RestaurantTransportLayer } from './store/RestaurantTransportLayer';
import { IRestaurantStore } from './store/IRestaurantStore';
import { viteEnvVar } from '@/utils/vite-env-var';

const BASE_URL = `https://api.outsidein.dev/${import.meta.env.VITE_API_KEY}`;
const VITE_API_KEY = viteEnvVar.get('VITE_API_KEY');
const BASE_URL = `https://api.outsidein.dev/${VITE_API_KEY}`;

export const RestaurantStoreContext = createContext<
IRestaurantStore | undefined
Expand Down
21 changes: 21 additions & 0 deletions src/utils/vite-env-var.ts
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`);
}
},
});
59 changes: 59 additions & 0 deletions tests/utils/vite-env-var.spec.ts
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'
);
});
});

0 comments on commit f422cf4

Please sign in to comment.