续集摩卡咖啡

时间:2018-10-31 14:03:43

标签: javascript node.js unit-testing mocha sequelize.js

我正在测试我的路线和控制器。这是我编写的演示测试,以查看一切是否正常运行。

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;

尽管我试图关闭服务器并关闭数据库连接,但是摩卡无法正常退出

因此,我包括了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是我的数据库服务器。

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

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