title: Mocking serverless-mysql using sinon
body: <p>I am trying to test an AWS lambda function (node.js) created using AWS SAM. My function uses the npm module <code>serverless-mysql</code> to connect to Aurora. The following are the relevant parts of my lambda function:</p>
  
  <pre><code>const connection = require('serverless-mysql')({
      config: {
          host     : process.env.DB_HOST,
          user     : process.env.DB_USER,
          password : process.env.DB_PASSWORD
      }
  });
  
  exports.lambdaHandler = async (event, context) =&gt; {
      try {
          const name = event.pathParameters.name;
          const rows = await connection.query('SELECT * FROM users WHERE name = ?', [name]);
          await connection.end()
  
          const user = rows[0];
          return {
              'statusCode': 200,
              'body': JSON.stringify({
                  firstName: user.first_name,
                  lastName: user.last_name,
                  bk: user.bk,
                  team: user.current_team
              })
          }
  
      } catch (err) {
          console.log(err);
          return err;
      }
  };
  </code></pre>
  
  <p>I am trying to tests this by mocking the <code>serverless-mysql</code> dependency, but I am currently unable to do so. My test looks like this:</p>
  
  <pre><code>const app = require('../../app.js');
  const chai = require('chai');
  const sinon = require('sinon');
  const expect = chai.expect;
  const event = {
      pathParameters: {
          name: 'johndoe'
      }
  }
  var context;
  
  var successConnectionObject = {
      connect: function() {
          return Promise.resolve();
      },
      query: function(sqlQuery, params) {
          return Promise.resolve('');
      },
      end: function() {} 
  }
  
  var mysql = require('serverless-mysql');
  var stub = sinon.stub(mysql, 'connect').returns(successConnectionObject);
  
  describe('Tests', function () {
      it('verifies successful response', async () =&gt; {
          const result = await app.lambdaHandler(event, context);
  
          expect(result).to.be.an('object');
          expect(result.statusCode).to.equal(200);
          mock.verify();
  
          mock.restore();
      });
  });
  </code></pre>
  
  <p>However, this returns the following error:</p>
  
  <pre><code>TypeError: Cannot stub non-existent own property connect
  </code></pre>
  
  <p>I believe this is because <code>mysql</code> is not instantiated. So I replaced the line <code>var mysql = require('serverless-mysql');</code> with:</p>
  
  <pre><code>var mysql = require('serverless-mysql')();
  </code></pre>
  
  <p>Unfortunately, this results in the following error:</p>
  
  <pre><code>AssertionError: expected [Error: Error: self signed certificate in certificate chain] to be an object
  </code></pre>
  
  <p>So it seems the <code>connect()</code> method of the real <code>serverless-mysql</code> module is called.</p>
  
  <p>How can I correctly mock <code>serverless-mysql</code> using sinon?</p>