title: Mocking just one function on a function array using Jest
body: <p>Here is my function</p>
  <pre><code>//functions.js
  const functions =
  {
      add: (a, b) =&gt; a + b,
      average: (a, b) =&gt; functions.add(a, b) / 2
  }
  
  
  module.exports = functions;
  </code></pre>
  <p>Here goes my test,</p>
  <pre><code>jest.mock(&quot;./functions&quot;); // this happens automatically with automocking
  
  const functions = require(&quot;./functions&quot;);
  
  test('calculate the average', () =&gt; {
  
      functions.add.mockImplementation((a, b) =&gt; a + b);
      expect(functions.average(2, 2)).toBe(2);
  
  })
  
  </code></pre>
  <p>I know how to do it on NestJS, at least for the context I am in. I am preparing a Udemy tutorial, and I would like to make a simple example.
  I would like to be able to double check if the method add is properly called, the arguments in properly called.</p>
  <p>The problem is that the automocking is mocking everything. I would like to just mock the add function, as so I know what is going on.</p>
  <p>I was able to find this solution, but I feel something is missing, when trying to transform theory in Jest.</p>
  <pre><code>//jest.mock(&quot;./functions&quot;); // this happens automatically with automocking
  
  const functions = require(&quot;./functions&quot;);
  
  test('calculate the average', () =&gt; {
  
      const spy = jest.spyOn(functions, 'add').mockImplementation((a, b) =&gt; console.log(&quot;I was called&quot;));
      //functions.add.mockImplementation((a, b) =&gt; a + b);
      functions.average(2, 2)
      //expect(functions.average(2, 2)).toBe(2);
      expect(spy.mock.calls[0][1]).toBe(2);
  })
  
  </code></pre>
  <p>Trying to implement a proposed solution, it makes sense, however, it does not work as I expected. I am unable to access the mock properties. The idea is that the information should be stored, as it does with conventional mocks, however, how can I access?</p>
  <pre><code>jest.mock('./functions', () =&gt; {
      // Require the original module to not be mocked...
      const originalModule = jest.requireActual('./functions');
  
      return {
          __esModule: true, // Use it when dealing with esModules
          ...originalModule,
          add: jest.fn((a, b) =&gt; a + b),
      };
  });
  
  const functions = require(&quot;./functions&quot;);
  
  test('calculate the average', () =&gt; {
  
      const add = require(&quot;./functions&quot;).add;
  
      functions.average(2, 2);
      expect(add.mock.calls[0][1]).toBe(2);
  
  })
  
  </code></pre>
  <p>tried also:</p>
  <pre><code>test('calculate the average', () =&gt; {
  
      const add = require(&quot;./functions&quot;).add;
      expect(functions.average(2, 2)).toBe(2);
  })
  
  </code></pre>
  <p>This showed me that the mock is &quot;local&quot;, even after mocking, <code>average</code> is calling the normal method, in the case of spy, it calls the mocked one. It seems the only solution is spy aftermath.</p>
  <p>P.S. I changed the mocked function in order to prove which function has been called.</p>