title: Mocking in jest and express
body: <p>I have some code in an Express route which talks to AWS Cognito and am having trouble working out how to mock it in tests.</p>
  
  <pre><code>cognitoExpress.validate(accessTokenFromClient, (err, response) =&gt; {
      if (err) return res.status(401).json({ error: err });
  
      res.json({ data: `Hello ${response.username}!` });
    });
  </code></pre>
  
  <p>Then in my test I want to say cognitoExpress.validate should be called once and return <code>{username: 'test user'}</code> so that it doesnt hit the network and doesnt actually call AWS Cognito</p>
  
  <pre><code>it('It should returns 200 with a valid token', async done =&gt; {
      const { cognitoExpress } = require('../helpers/cognitoExpress');
  
      // I have tried
      jest.mock('../helpers/cognitoExpress');
      // and this
      jest.mock('../helpers/cognitoExpress', () =&gt; ({
        validate: jest.fn()
      }));
  
      const token = 'sfsfdsfsdfsd';
      const response = await request.get('/').set('Authorization', token);
      expect(cognitoExpress.validate).toHaveBeenCalledWith(token);
      expect(response.body).toEqual({ data: 'Hello test user' });
      done();
    });
  </code></pre>
  
  <p>Thanks in advance....</p>