Skip to content

E2E template - close NestJS app after tests to prevent CI test runner from hanging #3293

@lrousseau-wanadev

Description

@lrousseau-wanadev

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

The E2E template generated by the NestJS CLI initializes the application in a beforeEach hook but never closes it. The missing app.close() call in an afterEach / afterAll hook prevents Jest from exiting cleanly, causing the test suite to hang indefinitely on CI.

Upstream reference: #1777 (closed without fixing the template — the maintainer's response states that adding app.close() in afterAll is "a personal choice", yet initializing in beforeEach is already an opinionated choice. The asymmetry leaves generated projects with a broken CI pipeline out of the box.)

Describe the solution you'd like

Add an afterAll or afterEach hook calling await app.close() to the generated E2E test template.

describe("AppController (e2e)", () => {
    let app: INestApplication<App>;

    beforeEach(async () => {
        const moduleFixture: TestingModule = await Test.createTestingModule({
            imports: [AppModule],
        }).compile();

        app = moduleFixture.createNestApplication();
        await app.init();
    });

    afterEach(async () => {
        await app.close();
    });

    it("/ (GET)", () => {
        return request(app.getHttpServer()).get("/").expect(200).expect("Hello World!");
    });
});

Teachability, documentation, adoption, migration strategy

Non-breaking change

What is the motivation / use case for changing the behavior?

The generated template is the first thing a developer runs when starting a new NestJS project. If the E2E test suite never terminates, the CI pipeline hangs indefinitely on the very first run, blocking any automated workflow built on top of the template (starter kits, monorepo generators, etc.).

Using --forceExit as a workaround is not acceptable in a generated template.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions