【问题标题】:How do I fix: "TypeError: wsModule.Server is not a constructor" when running tests in Jest如何修复:在 Jest 中运行测试时“TypeError:wsModule.Server 不是构造函数”
【发布时间】:2020-06-27 14:20:51
【问题描述】:

我是 Jest 的新手,我想开始为 Node.js 服务器编写一些集成测试。当我尝试运行测试时,我收到 "TypeError: wsModule.Server is not a constructor" 错误。为什么我的测试环境不会初始化套接字服务器?

server.js:

const app = require('express')();
const server = require('http').Server(app);
const io = require('socket.io')(server); <-- TEST FAILS BECAUSE OF SOCKET MODULE
const router = require('./router/router');
const bodyParser = require('body-parser');
const cors = require('cors');
require('./socket/socket')(io); 

// Allow CORS so our client can consume JSON
app.use(cors())

// Takes the raw requests and turns them into usable properties on req.body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// Use our router
app.use('/', router);

server.listen(3000, (req, res) => {
  console.log("listen at 3000!");
});

module.exports = app;

socket.js:

const userService = require('../services/userService');
const roomService = require('../services/roomService');

module.exports = function (io) {

  io.on("connection", socket => {

    console.log('there has been a connection with: ' + socket.id);

    socket.on('set-username', ({ roomId, username }) => {
      userService.setUsername(roomId, username, socket, io);
    });

    socket.on('start-game', ({ roomId, hostName }) => {
      roomService.startGame(roomId, hostName, socket, io);
    });

});

roomService.test.js:

const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('../../server');
const { expect } = chai;
chai.use(chaiHttp);

describe("example", () => {
  test('should run', () => {
    expect(true).to.be.true;
  })
})

【问题讨论】:

  • 你也试过const server = require('http').createServer(app);吗?
  • @Narigo 是的,不幸的是没有运气。

标签: node.js testing socket.io jestjs


【解决方案1】:

测试失败,因为server.listen异步,而您的测试是同步

为什么我的测试环境不会初始化套接字服务器?

问题是您的测试将在 HTTP 服务器启动之前完成,并且 Socket.IO 服务器已附加到它。

Testing Asynchronous Code

您可以在server.js 中评论server.listen 并导出server 以便您使用 它在测试时告诉jest 等到服务器启动然后运行其余的测试。

server.js

- server.listen(3000, (req, res) => { console.log("listen at 3000!"); });
- module.exports = app;

+ module.exports = server

roomService.test.js

const server = require('../../server');

describe("example", () => {
    beforeAll(done => { //pass a callback to tell jest it is async
        //start the server before any test
        server.listen(3000, () => done());
    })

    afterAll(done => { //pass a callback to tell jest it is async
        //close the server after all tests
        server.listening ? server.close(() => done()) : done();
    })

    test('should run', () => {
        expect(true).to.be.true;
    })
})

当然,当您想在开发模式下再次运行您的应用时,您必须取消注释。

为避免在此文件中注释/取消注释,请创建一个单独的模块,您可以在其中启动服务器并 使其成为应用入口点。

这是您的文件的样子

server.js

const app = require('./app');
const server = require('http').Server(app);
const io = require('socket.io')(server);
require('./socket/socket')(io); 
module.exports = server;

start.js:

const server = require('./server');
server.listen(3000, (req, res) => {
    console.log("listen at 3000!");
});

app.js

const app = require('express')();
const router = require('./router/router');
const bodyParser = require('body-parser');
const cors = require('cors');

app.use(cors())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/', router);

module.exports = app;

使用start.js 作为应用入口点,您现在无需进行任何更改即可运行应用和测试

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2019-10-04
    • 1970-01-01
    • 2018-07-01
    相关资源
    最近更新 更多