【发布时间】:2021-07-09 03:49:24
【问题描述】:
我正在使用 Jest、Supertest 和 Mongoose 作为端点在 NodeJS 中使用 MongoDB 编写后端项目的测试用例。目前我遇到错误UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:60524 并且测试失败。下面的代码是用异步方法编写的,它给出了我预期的实际结果。如果我用then().catch() 编写相同的逻辑,则没有错误,它表明一切正常,即使它应该是失败的测试用例。
#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);
});
});
});
我从 Jest 得到的错误是
● /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)
和 UnhandledPromiseRejectionWarning 错误重复。
(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.
我一直在研究这个问题来修复这个错误。我该如何解决这个问题?
【问题讨论】:
标签: node.js asynchronous mongoose jestjs supertest