开玩笑的EADDRINUSE ::: 3000

时间:2018-10-30 13:42:52

标签: jestjs

我知道此错误意味着端口3000已在使用中,但是我也尝试了其他端口,但是仅对于某些特定测试,我仍然遇到相同的错误。

这是其中一项测试的代码

@NodeEntity
public class Actor {
    Long id;
    @Relationship(type="PLAYED_IN") private Role playedIn;
}

@RelationshipEntity(type = "PLAYED_IN")
public class Role {
    @Id @GeneratedValue   private Long relationshipId;
    @Property  private String title;
    @StartNode private Actor actor;
    @EndNode   private Movie movie;
}

@NodeEntity
public class Movie {
    private Long id;
    private String title;
}

有什么办法解决此错误?

听EADDRINUSE ::: 3000

class Renderer {
constructor() {
    this.setUpAfterDelay();
}
setUp() {

    let width = Math.random() * 300;
    let height = Math.random() * 300;
    let top = Math.random() * 200;
    let left = Math.random() * 200;


    let shape = document.createElement("div");
    shape.style.width = width + "px";
    shape.style.height = height + "px";
    shape.style.position = "absolute";
    shape.style.top = top + "px";
    shape.style.left = left + "px";
    shape.style.backgroundColor = this.getRandomColor();
    document.body.appendChild(shape);

}
setUpAfterDelay() {
    setTimeout(this.setUp, Math.random() * 2000);

}

getRandomColor() {
    var letters = '0123456789ABCDEF';
    var color = '#';
    for (var i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
}
}

let renderer = new Renderer;

●/ api / returns›如果未提供customerId,则应返回400

  describe("GET /", () => {
    it("should return all genres", async() => {
      await Genre.collection.insertMany([
        {name: 'genre1'},
        {name: 'genre2'}
      ]);

      const res = await request(server).get('/api/genres');
      expect(res.status).toBe(200);
      expect(res.body.length).toBe(2);
      expect(res.body.some( g => g.name === 'genre1')).toBeTruthy();
      expect(res.body.some( g => g.name === 'genre2')).toBeTruthy();
    });
  });

这就是我在控制台中得到的内容

1 个答案:

答案 0 :(得分:0)

认为问题归结为在Jest的两次测试运行之间没有关闭服务器。

如果您使用jest --watch来运行测试,那么添加一个afterAll处理程序可能会有所帮助?

最小快递应用示例:

// app.js
const express = require("express");
const app = express();

// Server port
const HTTP_PORT = 3000;

// Start server
server = app.listen(HTTP_PORT, () => {
    //console.log(`Server running on port ${HTTP_PORT}`);
});

// Root endpoint
app.get("/", (req, res, next) => {
    res.json({"message":"Ok yah!"})
});

// Default response for any other request
app.use(function(req, res){
    res.status(404);
});

module.exports = server;

测试文件:

// app.test.js
const app = require('../app');

describe('GET /', () => {
  let response;

  it('proves Jest works', () => {
    expect(1).toBe(1);
  });

  it('proves maths still works', () => {
    expect(1+1).toBe(2);
  });

});

// remembering to CLOSE the server after tests completed.
afterAll(async () => {
  await app.close();
});