Skip to content

it doesn't close connection on module destroy #26

@stalniy

Description

@stalniy

Why

I want to db level testing. I create repository instance, pass inside drizzle db and when I try to destroy my test db after test finishes I get an error:

Error during teardown: error: database "test_notifications_1755143757200_notification_channel.repository" is being accessed by other users

Example code:

describe(NotificationChannelRepository.name, () => {
  const testDbService = new TestDatabaseService(expect.getState().testPath!);

  beforeAll(async () => {
    await testDbService.setup();
  });

  afterAll(async () => {
    await testDbService.teardown(); // <-- destroy test db
  });

  it("creates a default notification channel if it doesn't exist", async () => {
    const module = await setup();
    const repository = module.get(NotificationChannelRepository);

    const channel = await repository.createDefaultChannel({
      name: "Default",
      type: "email",
      userId: faker.string.uuid(),
      config: {
        addresses: ["test@test.com"]
      }
    });

  });

  let testModule: TestingModule | undefined;
  afterEach(async () => {
    await testModule?.close(); // <- destroys testModule
  });

  async function setup() {
    const module: TestingModule = await Test.createTestingModule({
      imports: [
        ConfigModule.forRoot({ isGlobal: true }),
        ...register(schema)
      ],
      providers: [
        NotificationChannelRepository,
      ]
    }).compile();
    testModule = module;

    return module;
  }
});

What

drizzle service should be destroyed, its connection to pg must be closed on application shutdown or module destroy

Workaround

It's possible to rely on any and use private methods of drizzle and run it afterEach:

 await testModule?.get(DRIZZLE_PROVIDER_TOKEN).session.client.end();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions