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

See original GitHub issue

I’m getting this message every time i’m using any libraries that depend on promises.

🐛 Bug Report

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.

To Reproduce

I have a function that need to make a request to external api, and in the same method just save in the database without waiting for a response.

I don’t want to wait until the saving process is done, but i’m forced to change the behaviour of my application to get it tested through jest., or i need to close the connection, stop the server for my code to work.

Expected behavior

Excecpted jest to stop and return to my console.

Link to repl or repo (highly encouraged)

line49 and line50

test("it should create new order", async () => {
  const response = await server.inject({
    method: "POST",
    url: "/api/orders",
    payload: JSON.stringify({
      customer: {
        email: "asd@gmail.com",
        phone: "20 51 75 95",
        city: "Aarhus",
        zip: "8000",
        first_name: "jamal",
        last_name: "soueidan"
      },
      properties: [
        {
          name: "custom engraving",
          value: "Happy Birthday Mom!"
        }
      ]
    })
  });

  expect(response.statusCode).toBe(200);
});

I had to make those changes to get jest working with my api server and mongodb. https://github.com/jamalsoueidan/giv-et-tilbud/commit/d8f326b6294f88d1f12136042d4adfdc83328201

Run npx envinfo --preset jest

  System:
    OS: Windows 10
    CPU: x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
  Binaries:
    npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:32
  • Comments:45 (1 by maintainers)

github_iconTop GitHub Comments

140reactions
nathanloyercommented, Sep 10, 2019

@lucasfcosta Every time I’ve tried using --detectOpenHandles in my test code in a few different applications over the years it has never provided me with any useful output. It just sits there waiting for everything to wrap up but without printing the warning that suggests to use the argument in the first place. Is there some trick to getting useful output from that argument that I am unaware of?

138reactions
dhurlburtusacommented, Jul 10, 2019

I was getting the same error in one of my integration tests. In it, I was using a Sequelize Model to setup the database to a known state in beforeAll or beforeEach. The fix was to call close on the Sequelize instance connected to the Model in the afterAll callback. Hope this helps someone else.

EDITED:

And by popular request, here is effectively what I’ve done:

# dbConnection.js
export default new Sequelize({...}); // The Sequelize instance.
# some.spec.js
import dbConnection from './dbConnection';

const { SomeModel } = dbConnection.models;

describe('...', () => {
  beforeEach(async () => {
      await SomeModel.create({...});
  });
  ...
});

afterAll(async done => {
  // Closing the DB connection allows Jest to exit successfully.
  dbConnection.close();
  done();
});

@Shrikant9 @AnitaMartinez

I added an example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest did not exit one second after the test run has completed ...
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 Fix: Jest Did Not Exit - DONN FELKER
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 >
Jest did not exit one second after the test run has ... - 인프런
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 >
Testing Asynchronous Code · Jest
It's common in JavaScript for code to run asynchronously. When you have code that runs asynchronously, Jest needs to know when the code...
Read more >
jest did not exit one second after the test run has ... - You.com
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 >

github_iconTop Related Medium Post

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