-1

I recently created a testing harness for this project I have been working on.

I got almost 100% testing coverage, but I am not sure how to test this .env file that I have.

> const dotenv = require("dotenv");
> 
> dotenv.config();
> 
> const audience = process.env.AUTH0_AUDIENCE; const domain =
> process.env.AUTH0_DOMAIN; const serverPort = process.env.SERVER_PORT;
> const clientOriginUrl = process.env.CLIENT_ORIGIN_URL; const secretKey
> = process.env.SECRET_KEY
> 
> if (!audience) {   throw new Error(
>     ".env is missing the definition of an AUTH0_AUDIENCE environmental variable",   ); }
> 
> if (!secretKey) {   throw new Error(
>     ".env is missing the definition of an AUTH0_AUDIENCE environmental variable",   ); }
> 
> if (!domain) {   throw new Error(
>     ".env is missing the definition of an AUTH0_DOMAIN environmental variable",   ); }
> 
> if (!serverPort) {   throw new Error(
>     ".env is missing the definition of a API_PORT environmental variable",   ); }
> 
> if (!clientOriginUrl) {   throw new Error(
>     ".env is missing the definition of a APP_ORIGIN environmental variable",   ); }
> 
> const clientOrigins = ["http://localhost:3000"];
> 
> module.exports = {   audience,   domain,   serverPort,  
> clientOriginUrl,   clientOrigins, };

This is what it looks like above. Any guidance would be appreciated, thank you!

Laysif Rana
  • 31
  • 1
  • 6

1 Answers1

1

Use process.env.XXX = test_value for each test case. You can catch and assert the error thrown in module scope in your test file. In order to test different branches of the code, we need to require the module multiple times. But there is a module registry cache for requiring module multiple times, in order to reset the module registry, we need to use jest.resetModules() in beforeEach hook.

index.js:

const dotenv = require('dotenv');

dotenv.config();

const audience = process.env.AUTH0_AUDIENCE;
const domain = process.env.AUTH0_DOMAIN;

if (!audience) {
  throw new Error('.env is missing the definition of an AUTH0_AUDIENCE environmental variable');
}

if (!domain) {
  throw new Error('.env is missing the definition of an AUTH0_DOMAIN environmental variable');
}

module.exports = { audience, domain };

index.test.js:

jest.mock('dotenv', () => ({ config: jest.fn() }), { virtual: true });

describe('71489696', () => {
  beforeEach(() => {
    jest.resetModules();
  });
  test('should throw error if AUTH0_AUDIENCE environment variable does not exist', () => {
    expect(() => require('.')).toThrowError(
      '.env is missing the definition of an AUTH0_AUDIENCE environmental variable'
    );
  });

  test('should throw error if AUTH0_DOMAIN environment variable does not exist', () => {
    process.env.AUTH0_AUDIENCE = 'test_audience';
    expect(() => require('.')).toThrowError('.env is missing the definition of an AUTH0_DOMAIN environmental variable');
    delete process.env.AUTH0_AUDIENCE;
  });

  test('should export environment variables', () => {
    process.env.AUTH0_AUDIENCE = 'test_audience';
    process.env.AUTH0_DOMAIN = 'test_domain';
    expect(require('.')).toEqual({ audience: 'test_audience', domain: 'test_domain' });
    delete process.env.AUTH0_AUDIENCE;
    delete process.env.AUTH0_DOMAIN;
  });
});

Test result:

 PASS  stackoverflow/71489696/index.test.js
  71489696
    ✓ should throw error if AUTH0_AUDIENCE environment variable does not exist (9 ms)
    ✓ should throw error if AUTH0_DOMAIN environment variable does not exist (2 ms)
    ✓ should export environment variables (1 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 index.js |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        3.628 s

You can do the rest environment variables like above.

Lin Du
  • 88,126
  • 95
  • 281
  • 483