title: Mock mysql connection with Jest
body: <p>Trying to test code that looks like this :</p>
  <pre><code>const mysql = require('mysql2/promise');
  
  async myFunction () {
  
      const db = await mysql.createConnection(options);
  
      const results = await db.execute('SELECT `something` from `table`;');
  
      await db.end();
      
      // more code ...
  
  }
  </code></pre>
  <p>I need to mock the the mysql connection in a way that will allow me to use whatever it returns to mock a call to the <code>execute</code> function.</p>
  <p>I have tried mocking the whole <code>mysql2/promise</code> module but of course that did not work, since the mocked <code>createConnection</code> was not returning anything that could make a call to the <code>execute</code>  function.
  I also tried only mocking these 3 functions that I need instead of mocking the whole module, something like:</p>
  <pre><code>jest.mock('mysql2/promise', () =&gt; ({
      createConnection: jest.fn(() =&gt; ({
          execute: jest.fn(),
          end: jest.fn(),
      })),
  }));
  </code></pre>
  <p>But that did not work too.
  Any suggestions are highly appreciated.</p>