title: Node.js and Mocha unit testing rollback transaction after each test
body: <p>I'm using <code>node v10.16.0</code> and npm packages <code>mssql 5.1.0</code> and <code>mocha 6.2.1</code>.
  What I want to do is write a sql unit test helper class (ES6) that handles transactions and rolls back after each unit test.</p>
  
  <p><strong>db.js</strong></p>
  
  <pre><code>const mssql  = require("mssql");
  const config = require("./config");
  
  class DbAccess{
      constructor(){
          this.connection = new mssql.ConnectionPool(config.dbConfig);
  
          this.connection.connect(err =&gt; {
          // ...
          })
  
          this.transaction = new mssql.Transaction(this.connection);
  
      }
  
      beginTransaction(){
          this.transaction.begin();
      }
  
  
      rollbackTransaction(){
          this.transaction.rollback();
      }
  
  
      executeSql (sql, callback) {
          this.connection.connect().then(() =&gt; {
              var req = new mssql.Request(conn);          
              req.query(sql).then((recordset) =&gt; {
                  callback(recordset);
              })
          .catch ((err) =&gt; {
              console.log(err);
              logger.error(err);
              callback(null, err);
          });})
          .catch ((err) =&gt; {
              console.log(err);
              logger.error(err);
              callback(null, err);
          });
      }
  }
  </code></pre>
  
  <p><strong>test.js</strong></p>
  
  <pre><code>before(() =&gt; {
      dbAccess.beginTransaction();
  });
  
  after(() =&gt; {
      dbAccess.rollbackTransaction();
  });
  describe('Test',function(){    
      describe('SelectAll()', function(){
          it('returns all',function(){                      
              dbAccess.insertrecords();                
          });
      });
  });    
  </code></pre>
  
  <p>That setup is not working as I'm getting a <code>UnhandledPromiseRejectionWarning: ConnectionError: Connection not yet open.</code></p>
  
  <p>I can't figure out how to </p>
  
  <ol>
  <li>Begin the transaction (in the before handler), </li>
  <li>Execute the sql queries (in the test, e.g. inserting records),</li>
  <li>Rollback the transaction (in the after handler)</li>
  </ol>