Skip to content
Latchkey

NestJS TestingModule missing app.init() in CI

A test created a Nest application from a TestingModule but never awaited app.init(), so global pipes, guards, interceptors, and onModuleInit hooks did not run. Requests then behave differently than in production.

What this error means

e2e tests fail because middleware or guards did not apply, or lifecycle hooks that set up state never ran, because await app.init() was omitted after createNestApplication.

NestJS
Expected: 200 (guard should allow)
Received: 500 (guard never registered)
Hint: createNestApplication() was called but app.init() was not awaited.

Common causes

createNestApplication without app.init()

The application object exists but its initialization phase, which registers global enhancers and runs init hooks, never executed.

The httpServer used before init completed

supertest was handed a server that had not finished initializing, so routes or middleware were not fully wired.

How to fix it

Await app.init() in beforeAll

  1. Create the app from the compiled TestingModule.
  2. Await app.init() before any request or assertion.
  3. Close the app in afterAll to release resources.
app.e2e-spec.ts
beforeAll(async () => {
  const moduleRef = await Test.createTestingModule({ imports: [AppModule] }).compile();
  app = moduleRef.createNestApplication();
  await app.init();
});

Apply the same globals as production

Register the global pipes and guards in the test app so behavior matches main.ts.

app.e2e-spec.ts
app.useGlobalPipes(new ValidationPipe());
await app.init();

How to prevent it

  • Always await app.init() after createNestApplication in tests.
  • Mirror global pipes and guards from main.ts in the test bootstrap.
  • Close the app in afterAll to avoid leaked handles between suites.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →