Skip to content
Advertisement

Invalidate node cache when using Jest

I have a file with object that gets populated with process.env properties:

env.js

console.log('LOADING env.js');

const {
  PROXY_PREFIX = '/api/',
  USE_PROXY = 'true',
  APP_PORT = '8080',
  API_URL = 'https://api.address.com/',
  NODE_ENV = 'production',
} = process.env;

const ENV = {
  PROXY_PREFIX,
  USE_PROXY,
  APP_PORT,
  API_URL,
  NODE_ENV,
};

module.exports.ENV = ENV;

Now I try to test this file with different process.env properties:

env.test.js

const envFilePath = '../../config/env';

describe('environmental variables', () => {
  const OLD_ENV = process.env;

  beforeEach(() => {
    process.env = { ...OLD_ENV };
    delete process.env.NODE_ENV;
  });

  afterEach(() => {
    process.env = OLD_ENV;
  });

  test('have default values', () => {
    const { ENV } = require(envFilePath);
    expect(ENV).toMatchSnapshot();
  });

  test('are string values (to avoid casting errors)', () => {
    const { ENV } = require(envFilePath);
    Object.values(ENV).forEach(val => expect(typeof val).toEqual('string'));
  });

  test('will receive process.env variables', () => {
    process.env.NODE_ENV = 'dev';
    process.env.PROXY_PREFIX = '/new-prefix/';
    process.env.API_URL = 'https://new-api.com/';
    process.env.APP_PORT = '7080';
    process.env.USE_PROXY = 'false';

    const { ENV } = require(envFilePath);

    expect(ENV.NODE_ENV).toEqual('dev');
    expect(ENV.PROXY_PREFIX).toEqual('/new-prefix/');
    expect(ENV.API_URL).toEqual('https://new-api.com/');
    expect(ENV.APP_PORT).toEqual('7080');
    expect(ENV.USE_PROXY).toEqual('false');
  });
});

Unfortunately, even though I try to load the file in every test separately the file gets loaded only once, making the third test fail with:

Expected value to equal:
  "dev"
Received:
  "production"

P.S. It doesn’t fail when I run the test alone.

I also know that env.js loads only once because console.log('LOADING env.js'); gets fired only once.

I tried to invalidate Nodes cache like:

  beforeEach(() => {
    delete require.cache[require.resolve(envFilePath)];
    process.env = { ...OLD_ENV };
    delete process.env.NODE_ENV;
  });

but require.cache is empty {} before each test so it seems that Jest is somehow responsible for importing the file.

I also tried to run yarn jest --no-cache but didn’t help.

So what I want is to load env.js before each test so I can test how it behaves with different node environmental variables.

jest@^22.0.4

Advertisement

Answer

You can use jest.resetModules() in beforeEach method to reset the already required modules

beforeEach(() => {
  jest.resetModules()
  process.env = { ...OLD_ENV };
  delete process.env.NODE_ENV;
});
Advertisement