I am using jest to test my node.js endpoints with supertest.
However after running my test jest does not exit, instead it returns the following and hangs:
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.
import request from "supertest";
import server from "../../src/index";
import mongoose from "mongoose";
describe("Authentication", () => {
beforeAll( async () => {
console.log("Test Starting...");
await mongoose.connection.dropDatabase();
});
afterAll( async () => {
console.log("... Test Ended");
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
});
it("should authenticate with basic auth", async (done) => {
const BASIC_AUTH = Buffer.from(TEST_VARIABLES.HS_USERNAME + ":" + TEST_VARIABLES.HS_PASSWORD).toString("base64");
const auth_response = await request(server).get("/api/v2/auth/admin")
.set("Authorization", "Basic " + BASIC_AUTH)
.expect(200);
done();
});
The app is the node.js server is still listening.
server = app.listen(this.config.port, "0.0.0.0");
adding server.close() to afterAll solved the problem.
afterAll( async () => {
console.log("... Test Ended");
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
app.close()
});