title: Mocking with Jest + Supertest in Typescript
body: <p>I am writing a mock test case with jest in typescript &amp; trying to mock API calls with supertest, but unable to get a mocked response in return, as I'm using Axios on the login function &amp; even tried to mock the Axios call but got no luck.</p>
  <p>Here's my code :</p>
  <p><strong>auth.controller.ts</strong></p>
  <pre><code>import { AxiosService } from &quot;helpers&quot;;
  
  export class AuthController {
      constructor() {
         ...
        // Some logic is written here
        this.init()
      }
  
      public init() {
         // prepared an route for login
         router.post('/api/auth/login', this.login);
      }
    
      login = async function (req: Request, res: Response): Promise&lt;void&gt; {
          // Preparing config for axios call with some logic
          ...
  
          // created a service for Axios call to some third party.
          AxiosService.call(req, res, config, &quot;login&quot;);
      }
    }
      
      
  </code></pre>
  <p><strong>auth.test.ts</strong></p>
  <pre><code>import { AuthController } from &quot;../../modules/auth/auth.controller&quot;;
  jest.mock(&quot;../../modules/auth/auth.controller&quot;);
  
  beforeAll(async () =&gt; {
    const auth = new AuthController();
    mockLogin = jest.spyOn(auth, &quot;login&quot;);
  });
  
  afterAll(async () =&gt; {
    server.stop();
  });
  
  test(&quot;should give login response&quot;, async () =&gt; {
      mockLogin.mockImplementation(() =&gt; {
        return Promise.resolve({ Success: true, body: &quot;Login&quot; });
      });
  
        const response = await request(server.app)
          .post(&quot;/api/auth/login&quot;)
          .send(requestBody)
          .expect(200);
  
        response.body // Getting the server response instead of mocked one
  })
      
      
  </code></pre>
  <p>Tried with this code too, but no luck over there :</p>
  <pre><code>jest.mock('../../modules/auth/auth.controller', () =&gt; {
    return { 
      AuthController: jest.fn().mockImplementation(() =&gt; {
            return {
                login: jest.fn()
            }   
        })
    }
  })
              
      
  </code></pre>
  <p>Here's my <strong>AxiosService</strong> class looks like :</p>
  <pre><code>export class AxiosService {
  
    public static async call(...):Promise&lt;void&gt; { 
      try { 
         const { data } = await axios(...);
  
         res.status(200).json(data);
      } catch(err) {
         res.status(400).send(err);
       }
    
    }
  </code></pre>
  <p>}</p>
  <p>Tried to mock <strong>AxiosService</strong> call method with the following line :</p>
  <pre><code>jest.mock('../../helpers/AxiosService', () =&gt; {
    return jest.fn().mockImplementation(() =&gt; {
      return { call: () =&gt; { return {success:true, data:'mock'}} }
    })
  })
  </code></pre>
  <p>However, after mocking the Axios call, I'm getting <strong>Async callback was not invoked within the 10000 (which I've given) ms timeout specified by jest.setTimeout</strong></p>
  <p>Anyone can help that would be great for me, as I'm new to the mocking concept so maybe I'm missing something here.</p>
  <p>Thanks in advance</p>