CodePark/ questions / Testing NestJS apps
START PRACTICINGPRACTICE
Backend question · Backend testing › Testing NestJS apps

What does await app.close() in a Nest e2e afterAll do?

Stops the HTTP server and runs shutdown lifecycle hooks such as onModuleDestroy, which is where modules like TypeORM close their connections

app.close() closes the server and runs the shutdown lifecycle hooks:
•
onModuleDestroy
•
beforeApplicationShutdown
•
onApplicationShutdown

Modules like TypeORM close their connections in these hooks, so the connection pools are released.


The option "Nothing, unless
app.enableShutdownHooks() was called in the test" is wrong. enableShutdownHooks() is only needed to run those hooks on OS signals like SIGTERM. A direct app.close() runs them anyway.

The option "Only stops the HTTP server" is wrong for the same reason: the hooks close the database connections too.


Forgetting
app.close() is a common reason why Jest reports open handles.