如何使用Jest库测试基本的socket.io Node.js应用程序?

时间:2019-06-15 17:55:23

标签: node.js socket.io mocha jestjs

我正在为我的react.js项目使用Jest测试库。我也想用它来测试我的Node.js应用程序,我已经尝试了很多示例,但仍然没有所有有趣的示例。

使用Mocha&Chai测试socket.io连接的一个示例起作用了,但是当我使用Jest时,它成功连接到了socket.io服务器,并且发出了事件,但无法监听。 在这两种情况下,我也都更新了socket.io软件包。

这是使用mocha&chai https://github.com/agconti/socket.io.tests

的示例

这是我的代码:

index.js

var express = require("express"),
  app = express(),
  port = process.env.PORT || 9000,
  http = require("http").Server(app),
  io = require("socket.io")(http);

io.on("connection", function(socket) {
  console.log("connected");

  socket.on("message", function(msg) {
    console.log("emitted");

    io.sockets.emit("message", msg);
  });
});

// export the server so it can be easily called for testing
exports.server = http.listen(port);

index.test.js

jest.setTimeout(50000);
var server = require("../index"),
  io = require("socket.io-client"),
  ioOptions = {
    transports: ["websocket"],
    forceNew: true,
    reconnection: false
  },
  testMsg = "HelloWorld",
  sender,
  receiver;

describe("Chat Events", function() {
  beforeEach(function(done) {
    // start the io server
    // server.start();
    // connect two io clients
    sender = io("http://localhost:9000/", ioOptions);
    receiver = io("http://localhost:9000/", ioOptions);

    // finish beforeEach setup
    done();
  });
  afterEach(function(done) {
    // disconnect io clients after each test
    sender.disconnect();
    receiver.disconnect();
    done();
  });

  describe("Message Events", function() {
    it("Clients should receive a message when the `message` event is emited.", function(done) {
      sender.emit("message", testMsg);
      receiver.on("message", function(msg) {
        console.log("subscribed");
        expect(msg).toEqual(testMsg);
        done();
      });
    });
  });
});

这是测试结果:

笑话

 console.log server/index.js:37
    connectedddd

  console.log server/index.js:40
    emitted

  console.log server/index.js:37
    connectedddd

  console.log server/index.js:37
    connectedddd

 FAIL  server/__tests__/index.test.js (58.299s)
  Chat Events
    Message Events
      ✕ Clients should receive a message when the `message` event is emited. (50105ms)

  ● Chat Events › Message Events › Clients should receive a message when the `message` event is emited.

    Timeout - Async callback was not invoked within the 50000ms timeout specified by jest.setTimeout.Error: 

      130 | 
      131 |   describe("Message Events", function() {
    > 132 |     it("Clients should receive a message when the `message` event is emited.", function(done) {
          |     ^
      133 |       sender.emit("message", testMsg);
      134 |       receiver.on("message", function(msg) {
      135 |         console.log("subscribed");

      at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
      at Suite.it (server/__tests__/index.test.js:132:5)
      at Suite.describe (server/__tests__/index.test.js:131:3)
      at Object.describe (server/__tests__/index.test.js:113:1)


MOCHA&CHAI


> node_socket_test@0.0.0 test /home/hassan/socket.io.tests
> mocha test



  Chat Events
    Message Events
connectedddd
connectedddd
subscribed
      ✓ Clients should receive a message when the `message` event is emited.


  1 passing (53ms)

1 个答案:

答案 0 :(得分:0)

您可以模拟客户端 socketEventChannel

在这里你可以看到一个有用的例子(不适用于这个特定的案例,而是为了理解这个概念):

https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/63605899

原始答案:https://stackoverflow.com/a/63770976/5192642