mercredi 17 mars 2021

How to write api request tests without launching server?

It is possible to launch server on port and test it with library "supertest". I'm wondering if it's possible to do the same without running a server?

Express app, the same as fastify app or others, takes request and response arguments from native node under the hood, like this:

const server = http.createServer((req, res) => {
  res.end('hello\n');
});

So it should be possible to call that callback directly from tests. How to get that callback? Is there any toolkit to help with constructing request and response objects for the callback?

The reason - just to make tests faster. As I don't want to test HTTP protocol and internals of node, I'd like to save time by skipping it.

Aucun commentaire:

Enregistrer un commentaire