title: Mocking Express Chained Response with JEST
body: <p>I am pretty new to the jest and typescript, I am trying to create a small unit test for a controller function in jest</p>
  <pre><code>import { Request, Response } from 'express';
  
  const healthCheck = (_req: Request, _res: Response) =&gt; {
    const value = _req.params.name ?? 'World!';
    return _res.status(200).json({ message: `Hello ${value}` });
  };
  
  export default healthCheck;
  </code></pre>
  <p>The unit test i wrote for the above function is</p>
  <pre><code>import { Request, Response } from 'express';
  
  import healthCheck from './health.controller';
  
  describe('Health Controller', () =&gt; {
    it('healthCheck should send 200 on an optional path param', () =&gt; {
      const req = { params: {} } as Request;
      const res = {
        json: jest.fn(),
        status: jest.fn(),
      } as unknown as Response;
  
      healthCheck(req, res);
  
      expect(res.status).toHaveBeenCalledWith(200);
    });
  });
  </code></pre>
  <p>I am getting an error</p>
  <pre><code>TypeError: Cannot read property 'json' of undefined
  &gt;  8 |   return _res.status(200).json({ message: `Hello ${value}` });
  </code></pre>
  <p>why i am getting the <code>json</code> of undefined, even if i mock the property?</p>