Jest did not exit one second after the test run has completed.
See original GitHub issueI’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)
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:
- Created 5 years ago
- Reactions:32
- Comments:45 (1 by maintainers)
Top Related StackOverflow Question
@lucasfcosta Every time I’ve tried using
--detectOpenHandlesin 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?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
beforeAllorbeforeEach. The fix was to callcloseon the Sequelize instance connected to the Model in theafterAllcallback. Hope this helps someone else.EDITED:
And by popular request, here is effectively what I’ve done:
@Shrikant9 @AnitaMartinez
I added an example.