title: Mocha test drains postgres connection pool
body: <p>I'm trying to run Mocha tests but I always get the error:</p>
  
  <pre><code>Unhandled rejection Error: pool is draining and cannot accept work
  at Pool.acquire (D:\Codes\Node\posgmvc\node_modules\generic-
  pool\lib\generic-pool.js:385:11)
  at D:\Codes\Node\posgmvc\node_modules\knex\lib\client.js:281:19
  at Promise._execute     (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\debuggability.js:299:9)
  at Promise._resolveFromExecutor         (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\promise.js:481:18)
  at new Promise     (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\promise.js:77:14)
  at Client_PG.acquireConnection     (D:\Codes\Node\posgmvc\node_modules\knex\lib\client.js:272:12)
  at D:\Codes\Node\posgmvc\node_modules\knex\lib\runner.js:199:23
  at Promise._execute     (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\debuggability.js:299:9)
  at Promise._resolveFromExecutor     (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\promise.js:481:18)
  at new Promise     (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\promise.js:77:14)
  at D:\Codes\Node\posgmvc\node_modules\knex\lib\runner.js:198:35
  at tryCatcher     (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\util.js:16:23)
  at Function.Promise.attempt.Promise.try     (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\method.js:39:29)
  at Runner.ensureConnection     (D:\Codes\Node\posgmvc\node_modules\knex\lib\runner.js:197:34)
  at Runner.run (D:\Codes\Node\posgmvc\node_modules\knex\lib\runner.js:47:42)
  at Builder.Target.then     (D:\Codes\Node\posgmvc\node_modules\knex\lib\interface.js:35:43)
  at Context.&lt;anonymous&gt; (D:\Codes\Node\posgmvc\test\article.js:21:30)
  at callFnAsync     (C:\Users\Ashutosh\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:366:21)
  at Hook.Runnable.run (C:\Users\Ashutosh\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:316:7)
  at next (C:\Users\Ashutosh\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:309:10)
  at Immediate._onImmediate (C:\Users\Ashutosh\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:339:5)
  at tryOnImmediate (timers.js:543:15)
  at processImmediate [as _immediateCallback] (timers.js:523:5)
  </code></pre>
  
  <p>Here is my test:</p>
  
  <pre><code>// During the test the env variable is set to test
  process.env.NODE_ENV = 'test';
  
  //Require the dev-dependencies
  var chai = require('chai'),
  chaiHttp = require('chai-http'),
  server = require('../app'),
  db = require('../app/db');
  
  var should = chai.should();
  
  chai.use(chaiHttp);
  
  var id;
  
  describe('Article URLs', function () {
  
  //Before each test we empty the database
  beforeEach(function (done) {
  
      db("articles").del().then(function (count) {
          console.log(count);
      }).finally(function () {
          db.destroy();
      });
  
      done();
  });
  
  describe('/POST article', function () {
      it('it should create new article', function (done) {
  
          chai.request(server)
              .post('/api/articles')
              .send({
                  title: 'test title',
                  url: 'some url',
                  text: 'some text'
              })
              .end(function (err, res) {
                  res.should.have.status(200);
                  res.should.be.json;
                  res.body.should.have.property('id');
                  res.body.should.have.property('message');
                  res.body.message.should.equals('done');
  
                  id = res.id;
  
                  done();
              });
      });
  });
  
  describe('/PUT article', function () {
      it('it should update article', function (done) {
  
          chai.request(server)
              .put('/api/articles/' + id)
              .send({
                  title: 'new title',
                  url: 'new url',
                  text: 'new text'
              })
              .end(function (err, res) {
                  res.should.have.status(200);
                  res.should.be.json;
                  res.body.should.have.property('id');
                  res.body.should.have.property('message');
                  res.body.message.should.equals('done');
                  done();
              });
      });
  });
  
  describe('/GET articles', function () {
      it('it should return all articles', function (done) {
          chai.request(server)
              .get('/api/articles')
              .end(function (err, res) {
                  res.should.have.status(200);
                  res.should.be.json;
                  res.body.should.be.a('array');
                  res.body.length.should.be.greaterThan(0);
                  res.body.should.have.property('articles');
                  res.body.should.have.property('message');
                  res.body[0].should.have.property('id');
                  res.body[0].should.have.property('title');
                  res.body[0].should.have.property('text');
                  done();
              });
      });
  });
  
  describe('/GET article', function () {
      it('it should return single article', function (done) {
          chai.request(server)
              .get('/api/articles/' + id)
              .end(function (err, res) {
                  res.should.have.status(200);
                  res.should.be.json;
                  res.body.should.have.property('article');
                  res.body.should.have.property('message');
                  res.body.should.have.property('id');
                  res.body.should.have.property('title');
                  res.body.should.have.property('text');
                  done();
              });
      });
  });
  
  describe('/DELETE article', function () {
      it('it should delete single article', function (done) {
          chai.request(server)
              .delete('/api/articles/' + id)
              .end(function (err, res) {
                  res.should.have.status(200);
                  res.should.be.json;
                  res.body.should.have.property('count');
                  res.body.should.have.property('message');
                  res.body.count.should.be.greaterThan(0);
                  res.body.message.should.be.equals('found');
                  done();
              });
      });
  });
  
  });
  </code></pre>
  
  <p>Here is my knexfile.js:</p>
  
  <pre><code>module.exports = {
  
  development: {
      client: 'postgresql',
      connection: {
          database: 'posgmvc-development',
          user:     'postgres',
          password: 'pa$$w0rd'
      },
      pool: {
          min: 2,
          max: 10
      }
  },
  
  test: {
      client: 'postgresql',
      connection: {
          database: 'posgmvc-test',
          user:     'postgres',
          password: 'pa$$w0rd'
      },
      pool: {
          min: 2,
          max: 10
      }
  }
  };
  </code></pre>