Skip to content

How to test Polka's middleware with supertest #63

@motss

Description

@motss

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);

});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions