question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prisma not disconnecting after jest run (NestJs project)

See original GitHub issue

Bug description

I am running end to end tests against a psql database using Jest in a NestJs project. The test runner does not seem to be closing the Prisma connection upon completion… I am seeing the following warning.

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

If I add the --detectOpenHandles flag i see the following: Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  TCPSERVERWRAP

      22 |   it('It should create a dummy org', async () => {
      23 |     const { body } = await request(app.getHttpServer())
    > 24 |       .post('/organizations')
         |        ^
      25 |       .expect(201);
      26 |     expect(body).toHaveProperty('organizationId');
      27 |   });

      at Test.Object.<anonymous>.Test.serverAddress (../node_modules/supertest/lib/test.js:61:33)
      at new Test (../node_modules/supertest/lib/test.js:38:12)
      at Object.obj.<computed> [as post] (../node_modules/supertest/index.js:27:14)
      at Object.<anonymous> (recreate.e2e-spec.ts:24:8)

This issue was not present when I was using TypeOrm, but after making the move to Prisma I started seeing it

The below is just additional information of a side effect caused by this, and is not part of the reproduction steps so I am not including the stack trace I was getting around this issue by using the forceExit flag so that my CI pipeline could at least pass, however I have gotten to a point where there are too many connections, and my tests start throwing errors. The following error is what I am seeing:

Error: Error querying the database: db error: FATAL: sorry, too many clients already

How to reproduce

  1. Clone the repository:
  2. run npm install
  3. run docker-compose up -d
  4. run npm run test:e2e

Expected behavior

The test runner should exit gracefully

Prisma information

This is all in the docker file included in the repo

Environment & setup

  • OS: Mac OS
  • Database: PostgreSQL
  • Node.js version: v12.10.0

The issue is also occurring on my circle ci build, using node: circleci/node@4.1 and image: ubuntu-1604:202007-01

Prisma Version

2.30.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
janpiocommented, Nov 12, 2021

Can you also reproduce this change in behavior @TasinIshmam with the most recent update?

2reactions
Brendan-Stubbscommented, Nov 10, 2021

@TasinIshmam I tried to incorporate this into my actual project, and it did not work. However I did see the notification to update to the new major version (To be precise ^3.4.2"). This seems to have fixed the problem 100% Without the need for your suggestion above.

Thanks very much for your help, I put this issue off for too long, and I am super happy I have finally overcome it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connecting and disconnecting (Concepts) - Prisma
This page explains how database connections are handled with Prisma Client and how to manually connect and disconnect your database.
Read more >
Jest and NestJs how how to close the prisma connections
Jest did not exit one second after the test run has completed. This usually means that there are asynchronous operations that weren't stopped...
Read more >
How to write tests for Prisma with Docker and Jest
For unit tests, create a Prisma client and disconnect after each test. For functional tests, start a server and close it after each...
Read more >
How to seed a database with Prisma and Next.js - PlanetScale
This project is already configured with Prisma (we'll look at the data models in a second). To work with this project and see...
Read more >
A Better Way To Run Integration Tests With Prisma and ...
Our method has the following benefits compared to the Prisma official ... Does not require you to write any teardown code in your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found