续档摩卡

Sequelize stalls mocha

我正在尝试测试我的路线和我的控制器。这是我写的一个演示测试,看看是否一切正常。

import chai from "chai";
import chaiHttp from "chai-http";
import chaiAsPromised from "chai-as-promised";
import sinon from "sinon";
import sinonChai from "sinon-chai";
import wtfnode from "wtfnode";
import UserRepo from "../repositories/userRepository";
import server, { stopServer } from "../app";
import db from "../models/index";

wtfnode.init();

chai.use(chaiHttp);
chai.use(chaiAsPromised);
chai.use(sinonChai);
chai.should();

describe("TEST", () => {
  describe("Happy Path", () => {
    before(() => {
      sinon.stub(UserRepo, "getUsers");
      const users = [
        { id: 1, first_name: "test", last_name: "test" },
        { id: 2, first_name: "test", last_name: "test" },
        { id: 3, first_name: "test", last_name: "test" },
        { id: 4, first_name: "test", last_name: "test" },
        { id: 5, first_name: "test", last_name: "test" }
      ];
      UserRepo.getUsers.returns(users);
    });
    it("Retrieves users from the database and renders an index page displaying all the users", done => {
      chai
        .request(server)
        .get("/admins/users")
        .end((err, res) => {
          res.should.have.status(200);
          UserRepo.getUsers.should.have.been.calledOnce;
          done();
        });
    });
    after(() => {
      UserRepo.getUsers.restore();
      stopServer();
      db.sequelize.close();
      wtfnode.dump();
    });
  });
});

下面的代码片段显示了我如何在 "app.js"

中导出服务器

const server = app.listen(port, () => {
  /* eslint-disable no-console */
  console.log(`App Running on port ${port}`);
  /* eslint-enable no-console */
});

export const stopServer = () => {
  console.log("Shutting the server");
  server.close();
};

export default server;

虽然我试图关闭服务器并关闭数据库连接,但 mocha 无法正常退出

所以我包括了 wtfnode 以查看哪些进程阻止 mocha 正常退出。 wtfnode报告如下:

[WTF Node?] open handles:
- File descriptors: (note: stdio always exists)
  - fd 1 (tty) (stdio)
  - fd 2 (tty) (stdio)
- Sockets:
  - (?:?) -> null:? (destroyed)
  - IP ADDRESS:52510 -> IP ADDRESS:3306
- Timers:
Unable to determine callsite for "bound". Did you require `wtfnode` at the top of your entry point?
  - (10000 ~ 10 s) bound  @ unknown:0
  - (10000 ~ 10 s) (anonymous) @ C:\GVW6\node_modules\generic-pool\lib\Pool.js:389
  - (9999 ~ 9 s) bound @ C:\GVW6\node_modules\generic-pool\lib\ResourceRequest.js:48

据我了解,端口 3306 上的进程 运行 正在阻止 mocha 退出,端口 3306 是我的数据库服务器。

如有任何帮助,我们将不胜感激

应用程序中有另一个脚本正在实例化 sequelize。该脚本没有关闭连接。在那里添加了关闭命令并解决了问题。感谢大家的帮助:)