gpt4 book ai didi

node.js - 如何修复 : "TypeError: wsModule.Server is not a constructor" when running tests in Jest

转载 作者:行者123 更新时间:2023-12-03 12:19:21 31 4
gpt4 key购买 nike

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

服务器.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;

套接字.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);
});

});

房间服务.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;
})
})

最佳答案

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

Why won't my test environment initialize the socket server?

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

参见 Testing Asynchronous Code

你可以在 server.js 中注释 server.listen 并导出 server 这样你就可以使用它在测试时告诉 jest 等到服务器启动后再运行其余测试。

服务器.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;
})
})

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

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

这是你的文件的样子

服务器.js

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

开始.js:

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

应用程序.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 应用程序入口点,您现在可以运行应用程序和测试而无需进行任何更改

关于node.js - 如何修复 : "TypeError: wsModule.Server is not a constructor" when running tests in Jest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62611226/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com