title: How to test nested mocked functions
body: <p>How can I write tests on mocked functions inside a mocked function? I want to test that my <code>publish</code> mocked function is called once. </p>
  
  <pre><code>jest.mock('amqplib', () =&gt; ({
    connect: jest.fn(() =&gt; Promise.resolve({
      createChannel: jest.fn(() =&gt; Promise.resolve({
        assertExchange: jest.fn(),
        publish: jest.fn(),
      })),
      close: jest.fn(),
    })),
  }));
  </code></pre>
  
  <p>The actual function I want to test.</p>
  
  <pre><code>export default function (key, data, exchange = 'portal.topic', options = { type: 'topic' }) {
    return amqp.connect(`amqp://${RABBITMQ_USER}:${RABBITMQ_PASS}@${RABBITMQ_URL}:${RABBITMQ_PORT}`).then(conn =&gt; {
      conn.createChannel().then((ch) =&gt; {
        ch.assertExchange(exchange, options.type, { durable: true });
        ch.publish(exchange, key, Buffer.from(JSON.stringify(data)));
      });
      setTimeout(() =&gt; { conn.close(); }, 1000);
    });
  }
  </code></pre>