I am writing test cases of a backend project in NodeJS with MongoDB by using Jest, Supertest and Mongoose for endpoints. Currently I am having the error UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:60524 and tests are getting failed. The code below is written in asynchronous method which gives actual result I expected. If I write the same logic with then().catch(), there's no error and it shows everything works even it should be the failed test case.
#gen.test.js
const request = require("supertest");
let server;
const { Genres } = require("../../models/genres");
describe("/api/genres", () => {
beforeEach(() => {
server = require("../../index");
});
afterEach(() => {
server.close();
Genres.deleteMany({}).then((res) =>
winston.info(`Deleted Documents ${res.deletedCount}.`)
);
});
describe("GET /", () => {
it("should return all the genres.", async () => {
await Genres.collection.insertMany([
{ name: "genre1" },
{ name: "genre2" },
{ name: "genre3" },
]);
const res = await request(server).get("/api/genres");
expect(res.status).toBe(200);
expect(res.body.length).toBe(3);
expect(res.body.some((g) => g.name === "genre1")).toBeTruthy();
});
});
//This one works with then().cath() . But If I make this to something that I don't expect, It still works.
describe("GET /id", () => {
it("should return a single genre", () => {
const genre = new Genres({ name: "genre1" });
genre
.save()
.then((res) => winston.info("new genre is saved for /id search"))
.catch((err) => winston.error(err.message));
request(server)
.get("/api/genres/" + genre._id)
.then((res) => {
expect(res.status).toBe(200);
expect(res.body).toHaveProperty("name", genre.name);
});
});
});
I got the error from the Jest is
● /api/genres › GET / › should return all the genres.
expect(received).toBe(expected) // Object.is equality
Expected: 200
Received: 500
26 |
27 | const res = await request(server).get("/api/genres");
> 28 | expect(res.status).toBe(200);
| ^
29 | expect(res.body.length).toBe(3);
30 | expect(res.body.some((g) => g.name === "genre1")).toBeTruthy();
31 |
at Object.<anonymous> (tests/integration/genres.test.js:28:26)
and UnhandledPromiseRejectionWarning Errors are repeated.
(node:31169) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:60524
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
(node:31169) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:31169) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:31169) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:60525
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
(node:31169) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:31169) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:60528
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
(node:31169) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:31169) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:60529
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
(node:31169) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 4)
A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks.
I have been doing research on this so long to fix this error. How can I solve this issue?