title: Test async function with jest
body: <p>I'm trying to mock the database connection object in order to do my tests but the error im getting is always the same.</p>
  
  <p>I already tried with another solutions but the mock is always returning me the same object. After a little reaserch and changes I found another solution like this</p>
  
  <pre class="lang-js prettyprint-override"><code>// connection.js
  /* istanbul ignore file */
  const mysql = require('mysql');
  
  let pool;
  const config = require('./config.js');
  
  const connection = getPool();
  /**
   * Singleton pool connection
   * @return {Pool} the pool connection
   */
  function getPool() {
    if (pool) {
      return pool;
    }
    pool = mysql.createPool(config);
    return pool;
  }
  
  module.exports = connection;
  </code></pre>
  
  <pre class="lang-js prettyprint-override"><code>// database.js
  const connection = require('./connection')
  function find() {
    return new Promise((resolve, reject) =&gt; {
      const query = 'select * from table`;
      connection.query(query, (err, res) =&gt; {
        if (err) {
          reject(new DatabaseError('Error'));
        }
        resolve(res);
      });
    });
  }
  </code></pre>
  
  <p>and finally my test file looks like this</p>
  
  <pre class="lang-js prettyprint-override"><code>
  const database = require('./database.js');
  const connection = require('./connection.js');
  const { expect } = chai;
  const DatabaseError = require('../../../src/errors/DatabaseError');
  
  jest.mock('./connection.js');
    describe('testing find all', () =&gt; {
      test('testing error ', async () =&gt; {
        connection.query.mockImplementation(() =&gt; (q,  cb) =&gt; cb(res, null, null));
        try {
          await database.find();
        } catch (e) {
        expect(err).to.be.an.instanceof(DatabaseError);
        }
      });
    });
  </code></pre>
  
  <p>The error im getting is always the same</p>
  
  <pre><code>    : Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout.Error:
  
        11 |   describe('test find all', () =&gt; {
      &gt; 12 |     test('testing error ', async () =&gt; {
  </code></pre>
  
  <p>I already tried using return statement and <code>async done</code> but nothing resolves this problem</p>
  
  <p>any help?</p>
  
  <p>The reason that I'm using <code>mockimplementation</code> is because i need to change every test that <code>query</code> value</p>