title: Mock a method of a service called by the tested one when using Jest
body: <p>I am trying to mock a method's service i export as a module from my test.
  This is something i use to do with "sinon", but i would like to use jest as much as possible.</p>
  
  <p>This is a classic test, i have an "authentication" service and a "mailer" service.</p>
  
  <p>The "authentication" service can register new users, and after each new registration, it ask the mailer service to send the new user a "welcome email".</p>
  
  <p>So testing the register method of my authentication service, i would like to assert (and mock) the "send" method of the mailer service.</p>
  
  <p>How to do that? Here is what i tried, but it calls the original mailer.send method:</p>
  
  <pre><code>// authentication.js
  
  const mailer = require('./mailer');
  
  class authentication {
    register() { // The method i am trying to test
      // ...
  
      mailer.send();
    }
  }
  
  const authentication = new Authentication();
  
  module.exports = authentication;
  
  
  // mailer.js
  
  class Mailer {
    send() { // The method i am trying to mock
      // ...
    }
  }
  
  const mailer = new Mailer();
  
  module.exports = mailer;
  
  
  // authentication.test.js
  
  const authentication = require('../../services/authentication');
  
  describe('Service Authentication', () =&gt; {
    describe('register', () =&gt; {
      test('should send a welcome email', done =&gt; {
        co(function* () {
          try {
            jest.mock('../../services/mailer');
            const mailer = require('../../services/mailer');
            mailer.send = jest.fn( () =&gt; { // I would like this mock to be called in authentication.register()
              console.log('SEND MOCK CALLED !');
              return Promise.resolve();
            });
  
            yield authentication.register(knownUser);
  
            // expect();
  
            done();
          } catch(e) {
            done(e);
          }
        });
      });
    });
  });
  </code></pre>