Reputation: 213
I got the following error by running the test case:
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.
I ran the test case with --ForceExit
as well as --detectOpenHandles
. But I didn't resolve this error.
Upvotes: 6
Views: 19786
Reputation: 628
@johnson lai's answer has a good explanation of how to ensure the asynchronous code is handled but, without seeing any of your code, it's hard to really find a solution for you.
I just spent a couple of hours dealing with this. The true problem is that if your test executes any other code that uses asynchronous code like a database connection, server, or anything similar, you need to mock it or ensure any connection is closed.
For me, I was testing an API endpoint that involved a server with NodeJS and a pg
module database connection. Despite properly handling the asynchronous code in the test, the database and server connection isn't handled in the test and remains open after the test finishes.
The solution: I mocked the pg
module and created a variable to reference the server instance. Then I did a setup and teardown with beforeEach
and afterEach
to create the server connection and close it for each test while the database was mocked.
Upvotes: 1
Reputation: 1
Upvotes: 0
Reputation: 1036
test("Fetch Token ", async done => {
await getToken("DEV", "myApp", token => {
console.log("AuthToken: ", authToken);
expect(authToken).toBeFalsy();
});
done();
});
2 possible reason:
getToken
used the wrong variable, instead of authToken
, it's token
.test("Fetch Token ", async done => {
await getToken("DEV", "myApp", token => {
console.log("AuthToken: ", token);
expect(token).toBeFalsy();
});
done();
});
jest.setTimeout(5000)
suggestion
When you use async-await you dont need to use done.
test("Fetch Token ", async done => {
const authToken = await getToken("DEV", "myApp", token => {
return token;
});
console.log("AuthToken: ", authToken);
expect(authToken).toBeFalsy();
done();
});
test("Fetch Token ", (done) => {
getToken("DEV", "myApp", token => {
console.log("AuthToken: ", token);
expect(token).toBeFalsy();
done();
});
});
Upvotes: 4