#Get port of e2e test

11 messages · Page 1 of 1 (latest)

latent swallow
#

During my e2e test I am using supertest together with the nestjs application. This is not starting a Webserver, because I want to collect coverage.

However I need to publish metadata (openid connect information) which includes also the URL of the service. Right now these values are defined in the constructor and passed via env.

So I see two approaches:

  • instead of a random port, bind it to a static one that is known (like 3000)
  • pass the value after the start to set it.

Creating the response based on the request by using the host paramter would be one approach, but I am not sure if this will work when using a proxy.

spiral moss
#

You can extract the port from the Express instance. I don't remember exactly, but app.getHttpServer() gives you the express instance, on which you could call server.address().port

#

But address() will only be available if you call app.listen(xxx) with a specified port. If you call app.init(), it will be null, because it doesn't really start an actual server

#

I think it's best to set a known port as an environment variable for e2e testing

latent swallow
#

I thought making a request to the server in the beforeall and the get the port via response.request.host would work. But it seems that it is using a new random port for every new request:

stdout | test/issuance.e2e-spec.ts > Issuance
[127.0.0.1:57646] /

stdout | test/issuance.e2e-spec.ts > Issuance > create oid4vci offer
[127.0.0.1:57648] /vci/offer

stdout | test/issuance.e2e-spec.ts > Issuance > create oid4vci offer
[127.0.0.1:57650] /session/be009687-bd47-443c-af67-dcadf040961b

stdout | test/issuance.e2e-spec.ts > Issuance > ask for an invalid oid4vci offer
[127.0.0.1:57652] /vci/offer

stdout | test/issuance.e2e-spec.ts > Issuance > get credential from oid4vci offer
[127.0.0.1:57654] /vci/offer
#

Since I want to get the coverage, start the app with .listen(3000 would give me the fixed port, but it will not be able to collect coverage

#

Nevermind, I read something wrong. I thought when running app.listen vitest would not be able to get to know which lines are executed.
But supertest that is used for this will start a webserver on an ephermal port when there is none specific. So running app.listen(3000) before will make sure the port is always the same. And coverage will be generated

#

Thank you @spiral moss for responding so fast!!!!

spiral moss
#

You would not be ale to collect coverage if you started the server in a different process than the one the test runs in.

#

Since you do it jn the same process, it doesn't matter if the requests go through the network layer, or if you call methods directly

#

But I'll give you another unprompted advice - generating coverage for e2e tests is a waste of resources (your effort, time and CI credits).