javascript - Sequelize Mocha 摊位

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

我正在尝试测试我的路线和 Controller 。这是我编写的演示测试,以查看一切是否正常工作。

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。该脚本没有关闭连接。在那里添加了关闭命令并解决了问题。谢谢大家的帮助:)

关于javascript - Sequelize Mocha 摊位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53085263/

相关文章:

javascript - knockout JS : Function call twice time

javascript - 匹配并不总是在一起的特定 URL 参数

javascript - 纬度/经度 -> X/Y, X/Y -> 纬度/经度

javascript - 如何将CARTO map 集成到react.js代码中

unit-testing - Golang模拟上下文 panic

javascript - 用正则表达式提取字符串

javascript - 是否可以在不执行 npm 安装的情况下只生成一个 package-lock.json 文件?

node.js - 从单个IP访问 Node

node.js - 调试 mocha 测试的阻力最小的途径是什么?

java - 如何在 mvcmock 单元测试中注入(inject)身份验证