Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 833
Jonah Avatar asked Jan 21 '26 06:01

Jonah


1 Answers

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.

like image 74
Jonah Avatar answered Jan 23 '26 19:01

Jonah