Skip to content

Commit

Permalink
test: ✅ add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmannZ committed Apr 30, 2023
1 parent ebb93c1 commit 8f793af
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# next-runtime-env - Runtime Environment Configuration

[![codecov](https://codecov.io/gh/expatfile/next-runtime-env/branch/main/graph/badge.svg?token=mbGgsweFuP)](https://codecov.io/gh/expatfile/next-runtime-env)

Populates your environment at **run-time** rather than **build-time**.

- Isomorphic - Server, browser and middleware compatible.
Expand All @@ -20,7 +22,7 @@ const {
configureRuntimeEnv();
```

This will generates a `__ENV.js` file that contains white-listed environment variables that have a `NEXT_PUBLIC_` prefix.
This will generates a `__ENV.js` file that contains allow-listed environment variables that have a `NEXT_PUBLIC_` prefix.

2. Add the following to the head section fo your `pages/_document.js`:

Expand Down
57 changes: 57 additions & 0 deletions src/helpers/get-public-env.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { getPublicEnv } from './get-public-env';

describe('getPublicEnv', () => {
afterEach(() => {
delete process.env.FOO;
delete process.env.BAR;
delete process.env.BAR;
delete process.env.NEXT_PUBLIC_FOO;
delete process.env.NEXT_PUBLIC_BAR;
delete process.env.NEXT_PUBLIC_BAZ;
});

it('should return a allow-listed value', () => {
process.env.NEXT_PUBLIC_FOO = 'foo';

expect(getPublicEnv()).toEqual({
NEXT_PUBLIC_FOO: 'foo',
});
});

it('should return multiple allow-listed values', () => {
process.env.NEXT_PUBLIC_FOO = 'foo';
process.env.NEXT_PUBLIC_BAR = 'bar';
process.env.NEXT_PUBLIC_BAZ = 'baz';

expect(getPublicEnv()).toEqual({
NEXT_PUBLIC_FOO: 'foo',
NEXT_PUBLIC_BAR: 'bar',
NEXT_PUBLIC_BAZ: 'baz',
});
});

it('should not return a non allow-listed value', () => {
process.env.FOO = 'foo';

expect(getPublicEnv()).toEqual({});
});

it('should not return multiple non allow-listed values', () => {
process.env.FOO = 'foo';
process.env.BAR = 'bar';
process.env.BAZ = 'baz';

expect(getPublicEnv()).toEqual({});
});

it('should not return a mixed list of allow-listed and non allow-listed values', () => {
process.env.NEXT_PUBLIC_FOO = 'foo';
process.env.BAR = 'bar';
process.env.NEXT_PUBLIC_BAZ = 'baz';

expect(getPublicEnv()).toEqual({
NEXT_PUBLIC_FOO: 'foo',
NEXT_PUBLIC_BAZ: 'baz',
});
});
});
74 changes: 74 additions & 0 deletions src/helpers/write-browser-env.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import fs from 'fs';

import { writeBrowserEnv } from './write-browser-env';

const infoSpy = jest.spyOn(console, 'info');

const base = fs.realpathSync(process.cwd());
const path = `${base}/public`;
const file = `${path}/__ENV.js`;

beforeAll(() => {
infoSpy.mockImplementation();

fs.mkdirSync(path);
});

afterAll(() => {
infoSpy.mockRestore();

fs.rmdirSync(path);
});

describe('writeBrowserEnv', () => {
afterEach(() => {
fs.rmSync(file);
});

it('should write an empty env', () => {
writeBrowserEnv({});

expect(infoSpy).toHaveBeenCalledWith(
'next-runtime-env: Writing browser runtime env',
file
);

const content = fs.readFileSync(file).toString();

expect(content).toEqual('window.__ENV = {};');
});

it('should write and env with a value', () => {
writeBrowserEnv({
NEXT_PUBLIC_FOO: 'foo',
});

expect(infoSpy).toHaveBeenCalledWith(
'next-runtime-env: Writing browser runtime env',
file
);

const content = fs.readFileSync(file).toString();

expect(content).toEqual('window.__ENV = {"NEXT_PUBLIC_FOO":"foo"};');
});

it('should write and env with multiple values', () => {
writeBrowserEnv({
NEXT_PUBLIC_FOO: 'foo',
NEXT_PUBLIC_BAR: 'bar',
NEXT_PUBLIC_BAZ: 'baz',
});

expect(infoSpy).toHaveBeenCalledWith(
'next-runtime-env: Writing browser runtime env',
file
);

const content = fs.readFileSync(file).toString();

expect(content).toEqual(
'window.__ENV = {"NEXT_PUBLIC_FOO":"foo","NEXT_PUBLIC_BAR":"bar","NEXT_PUBLIC_BAZ":"baz"};'
);
});
});
4 changes: 2 additions & 2 deletions src/helpers/write-browser-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function writeBrowserEnv(env: NodeJS.ProcessEnv) {
// eslint-disable-next-line no-console
console.info('next-runtime-env: Writing browser runtime env', path);

const populate = `window.__ENV = ${JSON.stringify(env)};`;
const content = `window.__ENV = ${JSON.stringify(env)};`;

fs.writeFileSync(path, populate);
fs.writeFileSync(path, content);
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* istanbul ignore file */

// This allows TypeScript to detect our global value.
declare global {
interface Window {
Expand Down

0 comments on commit 8f793af

Please sign in to comment.