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

标签 node.js socket.io mocha.js jestjs

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

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

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

这是我的代码:

索引.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();
      });
    });
  });
});

这是测试结果:

Jest
 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 和柴

> 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)

最佳答案

您可以模拟客户端 socketEventChannel
在这里,您可以看到一个有用的示例(不适用于此特定情况,但用于理解概念):
https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/63605899
原答案:https://stackoverflow.com/a/63770976/5192642

关于node.js - 如何使用 Jest 库测试基本的 socket.io Node.js 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56612836/

相关文章:

Node.Js - 获取 Windows 用户名

node.js - messages.sendMulticast 不是一个函数

javascript - Socket IO - NodeJS 需要外部脚本吗?

node.js - 使用 mocha 运行时,仍然收到 babel-plugin-syntax-dynamic-import 动态导入的语法错误

javascript - 超时 2000ms 超过 mocha

javascript - Node Webkit 如何将 div 的值保存为 .txt 文件

javascript - 依赖 Node.js require 行为来实现单例是否安全?

javascript - 如何将自定义数据传递给我的 Mocha 测试?

javascript - Socket.io 在浏览器导航到新页面之前接收消息

android - socket.io xhr 在连接缓慢时出现错误(3G 移动网络)