jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.06k stars 6.44k forks source link

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

Open jamalsoueidan opened 5 years ago

jamalsoueidan commented 5 years ago

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
Jesse-Yentsch commented 8 months ago

For those experiencing this when working with Firebase, you likely need to call testEnv.cleanup();

let testEnv;

beforeAll(async () => {
  // Silence expected rules rejections from Firestore SDK. Unexpected rejections
  // will still bubble up and will be thrown as an error (failing the tests).
  setLogLevel("error");
  testEnv = await initializeTestEnvironment({
    projectId: projectId,
    firestore: {
      rules: readFileSync("firestore.rules", "utf8"),
      host: "127.0.0.1",
      port: 8080,
    },
  });
});

afterAll(async () => {
    await testEnv.cleanup();
})