title: Invalidate node cache when using Jest
body: <p>I have a file with object that gets populated with <code>process.env</code> properties:</p>
  
  <p><strong>env.js</strong></p>
  
  
  
  <pre><code>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;
  </code></pre>
  
  <p>Now I try to test this file with different <code>process.env</code> properties:</p>
  
  <p><strong>env.test.js</strong></p>
  
  
  
  <pre><code>const envFilePath = '../../config/env';
  
  describe('environmental variables', () =&gt; {
    const OLD_ENV = process.env;
  
    beforeEach(() =&gt; {
      process.env = { ...OLD_ENV };
      delete process.env.NODE_ENV;
    });
  
    afterEach(() =&gt; {
      process.env = OLD_ENV;
    });
  
    test('have default values', () =&gt; {
      const { ENV } = require(envFilePath);
      expect(ENV).toMatchSnapshot();
    });
  
    test('are string values (to avoid casting errors)', () =&gt; {
      const { ENV } = require(envFilePath);
      Object.values(ENV).forEach(val =&gt; expect(typeof val).toEqual('string'));
    });
  
    test('will receive process.env variables', () =&gt; {
      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');
    });
  });
  </code></pre>
  
  <p>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:</p>
  
  <blockquote>
  <pre><code>Expected value to equal:
    "dev"
  Received:
    "production"
  </code></pre>
  </blockquote>
  
  <p>P.S. It doesn't fail when I run the test alone.</p>
  
  <p>I also know that <strong>env.js</strong> loads only once because <code>console.log('LOADING env.js');</code> gets fired only once.</p>
  
  <p>I tried to invalidate Nodes cache like:</p>
  
  <pre><code>  beforeEach(() =&gt; {
      delete require.cache[require.resolve(envFilePath)];
      process.env = { ...OLD_ENV };
      delete process.env.NODE_ENV;
    });
  </code></pre>
  
  <p>but <code>require.cache</code> is empty <code>{}</code> before each test so it seems that <strong>Jest</strong> is somehow responsible for importing the file.</p>
  
  <p>I also tried to run <code>yarn jest --no-cache</code> but didn't help.</p>
  
  <p>So what I want is to load <strong>env.js</strong> before each test so I can test how it behaves with different node environmental variables.</p>
  
  <p><strong>jest@^22.0.4</strong></p>