Search code examples
nestjstypeorm

E2e tests fail because of the extra opened database connection


I'm trying to setup environment for e2e tests. I setup e2e-database and module to load fixtures into it. But after all my manipulations I got next error:

AlreadyHasActiveConnectionError: Cannot create a new connection named "default", because connection with such name already exist and it now has an active connection session.

I don't understand where is the place where extra connection is created. Because in afterEach hook I destroy all created connections.

Here are some links to my code:

Stack:

UPD: I found a good example of nestjs project with typeorm and e2e tests. May be it will help someone. Fix for me was to use prisma in my current project. lol

UPD 2: I suppose that problem was in that fact that I didn't provide db connection to my custom repositories. Because of that every repo opens its own connection and it causes the error. The same error happend with prisma too. So, if you are using custom repos, provide db connection.

For example

{
  provide: UserDITokens.UserRepository,
  useFactory: () => new UserRepository(prisma),
},

Where prisma is singleton of db connection


Solution

  • I suppose that problem was in that fact that I didn't provide db connection to my custom repositories. Because of that every repo opens its own connection and it causes the error. The same error happend with prisma too. So, if you are using custom repos, provide db connection.

    For example

    {
      provide: UserDITokens.UserRepository,
      useFactory: () => new UserRepository(prisma),
    },
    

    Where prisma is singleton of db connection