title: Jest - Mock a constant property from a module for a specific test
body: <p>So, I'm attempting to do something which on the surface should be very simple...</p>
  <p>I have some constants defined in:</p>
  <pre class="lang-js prettyprint-override"><code>//constants.js
  
  module.exports = {
   MY_CONSTANT: &quot;TEST&quot;
  }
  </code></pre>
  <p>I have a file which I'm trying to test which has a branching statement like this:</p>
  <pre class="lang-js prettyprint-override"><code>//file to test
  //...
      
  if(CONSTANTS.MY_CONSTANT === &quot;TEST&quot;)
  {...}
  ...
  </code></pre>
  <p>And I have a test like this:</p>
  <pre class="lang-js prettyprint-override"><code>//test
  it(&quot;Should do something when MY_CONSTANT === &quot;TEST, () =&gt; {
    //This is fine as it is exported as TEST
  })
     
  it(&quot;Should do something else when MY_CONSTANT !== &quot;TEST, () =&gt; {
    //This seems annoyingly difficult to get working...
  })
  </code></pre>
  <p><a href="https://stackoverflow.com/questions/42977961/how-to-mock-an-exported-const-in-jest">I've tried this</a> - With no luck, it doesn't change the actual value</p>
  <p>I've tried changing the constant export to export an object instead (that didn't work)</p>
  <p>I've tried adding a <code>jest.mock(...)</code> for the constants in my test file and doing an unmock in the tests I don't need them mocked.</p>
  <p>I've tried adding a <code>jest.doMock(...)</code> within the test function I need to change the value. (along with <code>jest.resetModules</code> and another require)</p>
  <p>I've tried adding a <code>jest.doMock(...)</code> to a tests beforeEach (along with <code>jest.resetModules</code> and another require)</p>
  <p>I'm at a loss really...literally all I want to do is change a property value before a test runs </p>
  <p><strong>Update</strong><br />
  So I've done some of the suggestions made:</p>
  <p>I now have a <strong>mocks</strong> folder adjacent to the constant folder
  It contains a file named the same as the actual constants file and a custom export</p>
  <p>I've then added <code>jest.mock(&quot;../constants);</code> inside the test.</p>
  <p>I've then also added a <code>const funcImTesting = require(&quot;../../file&quot;).testFunction</code> inside the test.</p>
  <p>Still the constant remains unchanged and the test fails.</p>