Polka: How to test Polka's middleware with supertest

Created on 31 Aug 2018  路  3Comments  路  Source: lukeed/polka

Polka does not work well with supertest. Is there alternative yet simple way to test Polka's middleware or router handler in a similar fashion?

_FYI, I'm using Polka + TypeScript + Jest._

healthcheck.js

module.exports = function healthcheck() {
  return (_, res) => res.end('OK');
};

healthcheck.spec.js

// With `supertest` in Express...

const express = require('express'); // const polka = require('polka');
const supertest = require('supertest');

const healthcheck = require('../healthcheck.js');

describe('test-route', () => {
  it('returns', async (done) => {
    try {
     const app = express()); // const app = polka();
     const rq = supertest(app.use('/healthcheck', healthcheck()); // throw error when using `polka`

     rq.get('/healthcheck')
        .expect(200)
        .end((err, res) => {
          if (err) throw err;

          expect(res).toStrictEqual('OK');
          done();
        });
    } catch (e) {
      throw e;
    }
  }, 10e3);

});

Most helpful comment

Great, glad to hear it!

I'll make sure to add a clear note somewhere. Basically, anything that expects a express() will need to take polka().handler

I'll document this better in my next Polka push

All 3 comments

Hey,

Think supertest is expecting the app handler directly. Try this:

let { handler } = polka().use('/health', health check());
supertest(handler);

@lukeed Thank you so much for the solution. This works perfectly. Should this be included in the docs for future reference?

Great, glad to hear it!

I'll make sure to add a clear note somewhere. Basically, anything that expects a express() will need to take polka().handler

I'll document this better in my next Polka push

Was this page helpful?
0 / 5 - 0 ratings

Related issues

deadcoder0904 picture deadcoder0904  路  8Comments

bmcminn picture bmcminn  路  3Comments

lagmanzaza picture lagmanzaza  路  4Comments

lukeed picture lukeed  路  10Comments

ASaiAnudeep picture ASaiAnudeep  路  4Comments