title: How to access a mock method returned from a mocked library
body: <p>I'm mocking the <code>@elastic/elasticsearch</code> library and I want to test that the <code>search</code> method is called with the right arguments but I'm having issues accessing <code>search</code> from my tests.</p>
  <p>In my ES mock I just export an object that includes a <code>Client</code> prop that returns another object that has the <code>search</code> prop. This is the way <code>search</code> is accessed from the library</p>
  <pre class="lang-js prettyprint-override"><code>const { Client } = require('@elastic/elasticsearch')
  const client = new Client(...)
  client.search(...)
  </code></pre>
  <p><strong>__mocks__/@elastic/elasticsearch</strong></p>
  <pre class="lang-js prettyprint-override"><code>module.exports = {
      Client: jest.fn().mockImplementation(() =&gt; {
          return {
              search: (obj, cb) =&gt; {
                  return cb(
                      '',
                      {
                          statusCode: 200,
                          body: {
                              hits: {
                                  hits: [
                                      {
                                          _source: esIndexes[obj.index]
                                      }
                                  ]
                              }
                          }
                      }
                  )
              }
          }
      })
  }
  </code></pre>
  <p><strong>__tests__/getAddresses.test.js</strong></p>
  <pre class="lang-js prettyprint-override"><code>const { getAddresses } = require('../src/multiAddressLookup/utils/getAddresses')
  const { Client } = require('@elastic/elasticsearch')
  
  beforeEach(() =&gt; {
      process.env.ES_CLUSTER_INDEX = 'foo'
      process.env.ui = '*'
  })
  
  describe('multiAddressLookup', () =&gt; {
      test('Should return the correct premises data with only the relevant &quot;forecasted_outages&quot;', async () =&gt; {
          const event = {
              foo: 'bar'
          }
          const esQueryResponse = {
              &quot;body&quot;: &quot;\&quot;foo\&quot;:\&quot;bar\&quot;&quot;,
              &quot;headers&quot;: {&quot;Access-Control-Allow-Origin&quot;: '*'},
              &quot;statusCode&quot;: 200
          }
  
          await expect(getAddresses(event)).resolves.toEqual(esQueryResponse)
          expect(Client().search).toHaveBeenCalled()  // This fails with 0 calls registered
      })
  })
  </code></pre>