title: In jest, how do I mock an exported function to return a Promise rather than undefined?
body: <p>I'm using jest and Typescript.  I have this exported function in a service file ...</p>
  <pre><code>export async functionprocessData(
    data: MyDataI,
  ): Promise&lt;
      ...
  </code></pre>
  <p>Then in a separate file (run-my-process.ts) that I call via npm cli, I have this</p>
  <pre><code>import {processData } from '../services/my.service';
  ...
           processData(data)
              .then((result) =&gt; {
  </code></pre>
  <p>Now I would like to mock the &quot;processData&quot; function from jest, so I tried this</p>
  <pre><code>jest.mock('../services/my.service', () =&gt; {
    // The mock returned only mocks the generateServerSeed method.
    const actual = jest.requireActual('../services/my.service');
  
    return {
        ...actual,
       processData: jest.fn().mockReturnValue(Promise.resolve({
          dataInserted: 1,
        }))
      }
  });
  
  ...
  
  describe('calls the job', function () {
  
  
    it('invokes the function', async () =&gt; {
      ...
  
      jest.spyOn(process, 'exit').mockImplementationOnce(() =&gt; {
        throw new Error('process.exit() was called.');
      });
  
      expect(() =&gt; {
        require('./run-my-process');
      }).toThrow('process.exit() was called.');
  </code></pre>
  <p>But the test dies with the error</p>
  <pre><code>ERROR [1624482717612] (71310 on localhost): Cannot read property 'then' of undefined
  </code></pre>
  <p>So it seems my function, processData, is somehow evaluating to &quot;undefined&quot; when called with an argument.  What's the right way to mock my function to return a Promise and get my Jest test to pass?</p>