I'm new to Jest and I want to start writing some integration tests for a Node.js server. When I try and run a test, I receive a "TypeError: wsModule.Server is not a constructor"
error. Why won't my test environment initialise the socket server?
server.js:
const app = require('express')();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const router = require('./router/router');
const bodyParser = require('body-parser');
const cors = require('cors');
require('./socket/socket')(io); <-- TEST FAILS BECAUSE OF SOCKET MODULE
// 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;
})
})
Aucun commentaire:
Enregistrer un commentaire