title: If I am unit testing what happens inside an if-clause, should I mock everything up to the if-clause?
body: <h2>The scenario</h2>
  <p>I am writing unit tests for a module similar to the one below</p>
  <p><strong>service.js</strong></p>
  <pre><code>const client = require(&quot;./../client&quot;);
  
  let managedData = {};
  
  function manageData(dataId) {
    let users = client.retrieveUsers(); //Will return an array of strings. For example: a list of user names
    let user = client.retrieveCurrentUser(); //Will return a string. For example: one user name
    let userIsPresentInList = users.includes(user); //Check if the user is present in a broader user list
    if(userIsPresentInList) {
      managedData[dataId] = client.retrieveDataWithId(dataId);
    }
  }
  
  
  function unmanageData(dataId) {
    client.doSomethingToTheDataWithId(dataId)
    delete managedData[dataId]
  }
  
  module.exports = { 
    manageData,
    unmanageData
  }
  </code></pre>
  <p>Some of my unit tests target what happens when the user is present in the list of users, i.e., what happens inside the if-clause</p>
  <p>The questions is: if I am unit testing an if-clause, should I mock everything up to the clause (option 1 below)? Or should I create an way of &quot;directly&quot; ending up in the if-clause (option 2 below)?</p>
  <h2>The test</h2>
  <p><strong>Option 1) Group the tests via describe blocks and mock in beforeEach/beforeAll:</strong>: Scenarios that concern what's inside the if-clause are grouped in a describe block and mocks are done for all statements up until the if-clause</p>
  <p><strong>Option 2) Mock intermediate functions inside module</strong>: service.js is refactored. The code that checks whether the user is in the list of users is extracted in a new function inside the module. This function then is mocked. In this way anything before the if-clause does not really matter and the test only concerns what is inside the if-clause is working</p>
  <p><strong>service.test.js</strong></p>
  <pre><code>const service = require(&quot;./service&quot;);
  const client = require(&quot;./../client&quot;);
  jest.mock(&quot;./../client&quot;);
  
  beforeEach(() =&gt; {
    jest.clearAllMocks();
  });
  
  describe('Sucesful management', () =&gt; {
  
  
      //OPTION 1
      beforeEach(() =&gt; {
          client.retrieveUsers = jest.fn(() =&gt; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]);
          client.retrieveCurrentUser = jest.fn(() =&gt; &quot;a&quot;);
      })
  
      //OPTION 2
      beforeEach(() =&gt; {
          service.isUserPresentInUsersList = jest.fn(() =&gt; true);
      })
  
      afterEach(() =&gt; {
        service.isUserPresentInUsersList.mockRestore();
      })
  
      it(&quot;Given the user belongs to the user list, should retrieve the data calleing manageData&quot;, async () =&gt; {
          //Act
          service.manageData(&quot;D&quot;);
          //Assert
          expect(client.retrieveDataWithId).toHaveBeenCalledWith(&quot;D&quot;);
        });
      
        it(&quot;Given the user belongs to the user list, should do something with the data when calling unmanageData&quot;, async () =&gt; {
          //Act
          service.manageData(&quot;D&quot;);
          service.unmanageData(&quot;D&quot;);
          //Assert
          expect(client.doSomethingToTheDataWithId).toHaveBeenCalledWith(&quot;D&quot;);
        });
  })
  </code></pre>
  <p>Is there preferable one in this case? Is any of those approaches terrible?</p>
  <p>An extended question would be: what are open source projects that have good testing practices that I can get inspiration from?</p>