title: Jest mockImplementation(Promise.resolve) returns undefined when spying on an object called from another class (Node.js)
body: <p>I'm having trouble getting a Jest mockImplementation to return data with Promise.resolve() in a particular context.</p>
  <p>In my Node.js code, I have a structure that puts all the API-calling code in one class (Api), and all the business logic in another (Repo). When you instantiate Repo, it creates an instance of the Api class for itself: <code>this.api = new Api()</code></p>
  <p>I'm trying to test a function in the Repo level, and mocking the Api call so we don't actually make any queries during testing.</p>
  <pre><code>  const data = { postId: 3, id: 3, name: 'Bob Your Uncle' };
    beforeEach(() =&gt; {
      getSpy = jest.spyOn(repo.api, 'getPage').mockImplementation(() =&gt; {
        Promise.resolve(data);
      });
    });
  </code></pre>
  <p>When I execute the function, <code>expect(getSpy).toHaveBeenCalledTimes(1)</code> returns true, but the value it returns is &quot;undefined&quot; rather than the expected promise resolved.</p>
  <p>When I do a real-life run of the code, it works just fine, but the test fails. I suspect the problem may have to do with the fact that the code I'm testing is calling a second user-written class, by I'm not sure. I found this question <a href="https://stackoverflow.com/questions/65151813/jest-spy-on-object-method">Jest: Spy on object method?</a> that seems related, but their solution (adding .prototype to the spyOn argument) didn't help me -- they were working with a native JS object rather than a user-defined one, which seems different enough to explain the difference.</p>
  <p>Here is the full <code>describe</code> block:</p>
  <pre><code>describe('Repo: fetchRand', () =&gt; {
    const data = { postId: 3, id: 3, name: 'Bob Your Uncle' };
    beforeEach(() =&gt; {
      getSpy = jest.spyOn(repo.api, 'getPage').mockImplementation(() =&gt; {
        Promise.resolve(data);
      });
    });
    afterEach(() =&gt; {
      jest.clearAllMocks();
    });
    it('should fetch call this.api.getPage once', async () =&gt; {
      let res = await repo.fetchRand();
      expect(getSpy).toHaveBeenCalledTimes(1);
    });
    it('should return an object with a postId and an name', async () =&gt; {
      let res = await repo.fetchRand();
      expect(res).toHaveProperty('postId');
      expect(res).toHaveProperty('name');
    });
  });
  </code></pre>
  <p>and the module being tested:</p>
  <pre><code>const Api = require('./Api');
  
  module.exports = class Repository {
    constructor() {
      this.api = new Api();
    }
    async fetchRand() {
      let id = this.getRandomInt(5);
      let res = await this.api.getPage(id);
      return res;
    }
    getRandomInt(max) {
      return Math.floor(Math.random() * max) + 1;
    }
  };
  </code></pre>
  <p>And a link to <a href="https://github.com/kenirwin/jest-mock-problem-StackOverflow" rel="nofollow noreferrer">a GitHub repo with the full code</a> -- run <code>npm test -- Repo.test.js</code> to replicate the problem. (This is not my real project, just the simplest full code to replicate the basic nature of the problem.)</p>