Search code examples
node.jskoasupertest

Koa app hangs when tested with supertest


My supertest / tape test file looks like this:

var test    = require('tape');
var app     = require('../../api');
var agent = require('supertest').agent
var supertestCompatibleServer = agent(app.callback());

test('GET /Campus.svc', function (t) { 

  supertestCompatibleServer
  .get('/Campus.svc')
  .expect(200)
  .expect('Content-Type', /json/)
  .end(function (err, res) {
    t.ifError(err, 'No error');
    t.end();
  });
});
  1. The endpoint I'm testing works fine when starting the server and manually hitting it with curl or the browser.
  2. The tests run fine and pass, but they just hang at the end instead of finishing.
  3. The actual endpoint code just hits the database and returns some records as json.

What could be causing the tests to hang and how can I fix it?


Solution

  • This turned out to be related to this issue: https://github.com/substack/tape/issues/216

    In my case, the database connection via knex was still open, which was causing node process to finish. The solution was to explicitly call knex.destroy() in a teardown test.