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);
});
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
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 takepolka().handlerI'll document this better in my next Polka push