Skip to content

Fix hanging yarn test:e2e by adding proper connection lifecycle management#27

Merged
runephilosof-abtion merged 1 commit into
mainfrom
copilot/fix-yarn-test-e2e-hanging
Jul 2, 2026
Merged

Fix hanging yarn test:e2e by adding proper connection lifecycle management#27
runephilosof-abtion merged 1 commit into
mainfrom
copilot/fix-yarn-test-e2e-hanging

Conversation

Copilot AI commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Jest was hanging indefinitely after e2e tests because MongoDB and Redis connections were never closed — the event loop stayed alive waiting for them. --forceExit was masking the root cause.

Changes

test/app.e2e-spec.ts

  • beforeEachbeforeAll; added afterAll(() => app.close()) to trigger the NestJS shutdown lifecycle

src/common/mongo/mongo.module.ts

  • Splits the single factory into a MONGO_CLIENT provider (returns the MongoClient) and MONGODB derived from it
  • Adds MongoClientService implements OnApplicationShutdownclient.close()
  • Previously the MongoClient reference was lost inside the factory, making cleanup impossible

src/common/redis/redis.module.ts

  • Adds RedisCleanupService implements OnApplicationShutdownclient.disconnect()

src/knowledgebase/websocketchat.gateway.ts

  • WebSocketChatGateway creates its own private Redis client; implements OnApplicationShutdown to disconnect it

.github/workflows/package-tests.yml

  • Removes --forceExit

Pattern used

@Injectable()
class MongoClientService implements OnApplicationShutdown {
  constructor(private readonly client: MongoClient) {}
  async onApplicationShutdown() {
    await this.client.close();
  }
}

NestJS calls onApplicationShutdown on all providers when app.close() is invoked, which is now guaranteed by the afterAll teardown.

…hanging e2e tests

- MongoModule: split into MONGO_CLIENT + MONGODB providers; add MongoClientService
  implementing OnApplicationShutdown to close the MongoClient on teardown
- RedisModule: add RedisCleanupService implementing OnApplicationShutdown to
  disconnect the ioredis client on teardown
- WebSocketChatGateway: implement OnApplicationShutdown to disconnect its
  private ioredis client on teardown
- test/app.e2e-spec.ts: switch beforeEach → beforeAll and add afterAll that
  calls app.close(), triggering the shutdown lifecycle
- CI: remove --forceExit from yarn test:e2e
@runephilosof-abtion
runephilosof-abtion marked this pull request as ready for review July 2, 2026 07:49
@runephilosof-abtion
runephilosof-abtion merged commit 37c3712 into main Jul 2, 2026
3 checks passed
@runephilosof-abtion
runephilosof-abtion deleted the copilot/fix-yarn-test-e2e-hanging branch July 2, 2026 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants