Skip to content

Instantly share code, notes, and snippets.

@anchnk
Created November 5, 2018 11:17
Show Gist options
  • Save anchnk/2eb294cf9fce78bf63d0a7d350302b8d to your computer and use it in GitHub Desktop.
Save anchnk/2eb294cf9fce78bf63d0a7d350302b8d to your computer and use it in GitHub Desktop.
Jest environment variable testing

Simple example that demonstrates how to test environment variables from Jest

describe('models/env unit tests', () => {
  function importTestedModule() {
    return require('../../../src').models; // eslint-disable-line
  }

  beforeEach(() => {
    jest.resetModules();
  });

  test('env is set', () => {
    process.env.TABLE_NAME = 'foo';
    const { env } = importTestedModule();
    const result = env.getTableName();
    expect(result).toEqual('foo');
  });

  test('env is not set', () => {
    delete process.env.TABLE_NAME;
    const { env } = importTestedModule();
    const result = env.getTableName();
    expect(result).toEqual('default-table-name');
  });
});
const TableName = process.env.TABLE_NAME || 'default-table-name';

function getTableName() {
  return TableName;
}

module.exports = {
  getTableName
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment