Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test Polka's middleware with supertest #63

Closed
motss opened this issue Aug 31, 2018 · 3 comments
Closed

How to test Polka's middleware with supertest #63

motss opened this issue Aug 31, 2018 · 3 comments

Comments

@motss
Copy link

motss commented Aug 31, 2018

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

});
@lukeed
Copy link
Owner

lukeed commented Aug 31, 2018

Hey,

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

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

@motss
Copy link
Author

motss commented Sep 3, 2018

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

@lukeed
Copy link
Owner

lukeed commented Sep 3, 2018

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

@lukeed lukeed closed this as completed in 99f4fe9 Sep 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants