节点Mocha和Supertest:错误:ECONNREFUSED:连接被拒绝

时间:2018-06-29 10:03:47

标签: node.js mocha chai supertest

我已经构建了一个api,我想测试一些端点。

我有许多与以下测试相似的测试。它们都因Error: ECONNREFUSED: Connection refused

而失败

测试

import { assert, expect } from "chai";
import request from "supertest";
import app from "./../src/index";

describe("Authentication", () => {

    it("should respond with 200 product_id is authorised", async () => {

        const result = await request(app).post("/api/auth")
            .send({
                product_id: "123",
                origin: "localhost:3000",
            })
            .expect("Content-Type", /json/)
            .expect(200);

    });

    it("should respond with session token", async () => {

        const result = await request(app).post("/api/auth")
            .send({
                product_id: "123",
                origin: "localhost:3000",
            });

        expect(result.body.data).to.have.property("token");

    });
});

package.json

"test": "mocha -r ts-node/register --project tsconfig.json test/*.test.ts --exit"

错误:

> mocha -r ts-node/register --project tsconfig.json test/*.test.ts --exit

Server Running On: runner-sefsf-project-41-concurrent-0gdrs7:3000


  Authentication
MongoDB Successfully Connected On: mongodb://localhost:27017/p
    1) should respond with 200 product_id is authorised
    2) should respond with p session token

  Server
    3) should be up
    4) should throw 404 for unrecognized routes

  Transaction
    5) should respond with a new transction


  0 passing (40ms)
  5 failing

  1) Authentication
       should respond with 200 product_id is authorised:
     Error: ECONNREFUSED: Connection refused
      at Test.assert (node_modules/supertest/lib/test.js:165:15)
      at assert (node_modules/supertest/lib/test.js:131:12)
      at /eng/p-server/node_modules/supertest/lib/test.js:128:5
      at Test.Request.callback (node_modules/superagent/lib/node/index.js:718:3)
      at ClientRequest.req.once.err (node_modules/superagent/lib/node/index.js:646:10)
      at Socket.socketErrorListener (_http_client.js:382:9)
      at emitErrorNT (internal/streams/destroy.js:82:8)
      at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
      at process._tickCallback (internal/process/next_tick.js:63:19)

  2) Authentication
       should respond with p session token:
     Error: ECONNREFUSED: Connection refused
      at Test.assert (node_modules/supertest/lib/test.js:165:15)
      at assert (node_modules/supertest/lib/test.js:131:12)
      at /eng/p-server/node_modules/supertest/lib/test.js:128:5
      at Test.Request.callback (node_modules/superagent/lib/node/index.js:718:3)
      at ClientRequest.req.once.err (node_modules/superagent/lib/node/index.js:646:10)
      at Socket.socketErrorListener (_http_client.js:382:9)
      at emitErrorNT (internal/streams/destroy.js:82:8)
      at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
      at process._tickCallback (internal/process/next_tick.js:63:19)

显然连接被拒绝了吗?但是我不确定这到底意味着什么,因为您可以看到错误日志中的服务器已连接,mongo服务也已连接。

index.ts

import Server from "./server";

export default new Server().server;

server.ts

从“ ./app”导入应用;

class Server {

    public server: any;
    private instance: any;
    private app: any;
    private config: any;

    constructor() {

        this.instance = new App();
        this.app = this.instance.app;
        this.config = this.instance.config;
        this.server = this.app.listen(this.config.port, this.config.hostname);

        console.log("Server Running On: " + this.config.hostname + ":" + this.config.port);

    }

}

export default Server;

2 个答案:

答案 0 :(得分:2)

在使用com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Amir Hessam Koohestani' Courses (Title VARCHAR(30), Unit text, Primary key (Tit' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359) at XVI.SQLManager.createTable(SQLManager.java:35) at Panels.Units.lambda$Show$1(Units.java:702) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6533) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) 来运行测试用例之前,请确保服务器已停止,因为supertest在同一端口中运行api。因此,您需要使该端口免费供supertest使用。

由于您正在测试文件中使用它,

supertest

import request from "supertest"; import app from "./../src/index"; 包含域URL,例如app,当您调用api之类的http://localhost:3135时,其中request(app).post指的是request模块,需要确保supertest是免费的。也就是说,app在端口request('http://localhost:3135').post上没有进程运行时起作用。

您可以使用3135(在Linux中)检查正在运行节点的进程,并终止每个进程以确保端口可用或终止该特定端口的进程。

答案 1 :(得分:0)

我遇到了相同的错误,花了几个小时试图弄清楚是怎么回事。我正在使用$echo "node_modules" >> .gitignore $git rm -r --cached node_modules $git commit -am 'untracked node_modules' 库,因此碰巧它需要从环境变量中选择db选项。我的问题是我在错误的地方打了pg