P
P
PQR2018-10-16 19:49:43
Software testing
PQR, 2018-10-16 19:49:43

How to properly test an Express application? Is running an http server normal or not?

Question: Can I test express application without running http server?
Googled “how to test express app”, a lot of examples on mocha + chai + chai-http.
However, upon closer examination, I found that chai-http under the hood quickly creates a server (starts `.listen()`), and at the end of the test stops the server (.close()).
Quoting from the documentation: ( https://www.chaijs.com/plugins/chai-http/)
You may use a function (such as an express or connect app) or a node.js http(s) server as the foundation for your request. If the server is not running, chai-http will find a suitable port to listen on for a given test.
When passing an app to request; it will automatically open the server for incoming requests (by calling listen()) and, once a request has been made the server will automatically shut down (by calling .close()).

The question arose from my PHP background: there, to test an application, they usually don’t run a real HTTP server listening on a port, because Symfony / Laravel / ... an application can slip a pre-formed stub request, get a response and check its contents, while the tcp stack does not involved, no underhood .listen() / .close(). It feels more reliable as I want to test exactly the routing and handlers of my application's handles, without involving the network stack and a real http server.
Does this desire make sense, is this practiced with express applications? Or is it normal practice to set up an http server for tests of an express application and don't worry, because the http server “just works” and there are no problems with running it in tests?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2018-10-16
@PQR

I do not see any problems associated with using the http server during tests. Moreover, the express itself is such a server, and all its routers are tied to it, receiving a bunch of information in the request, including http headers and much more. Theoretically, it is probably possible to create a testing system that works without running a real server, but for this, at least you will have to write your own bicycle, which will pretend that it is an http server and slip it into express instead of a real one. Moreover, such a bike will have to fully emulate the api of the http module. Perhaps such a great even exists, but it all seems to me much more unreliable and meaningless. Though of course it's up to you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question