title: How do I fix: "TypeError: wsModule.Server is not a constructor" when running tests in Jest
body: <p>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 <code>&quot;TypeError: wsModule.Server is not a constructor&quot;</code> error. Why won't my test environment initialise the socket server?</p>
  <p>server.js:</p>
  <pre><code>const app = require('express')();
  const server = require('http').Server(app);
  const io = require('socket.io')(server); &lt;-- 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) =&gt; {
    console.log(&quot;listen at 3000!&quot;);
  });
  
  module.exports = app;
  </code></pre>
  <p>socket.js:</p>
  <pre><code>const userService = require('../services/userService');
  const roomService = require('../services/roomService');
  
  module.exports = function (io) {
  
    io.on(&quot;connection&quot;, socket =&gt; {
  
      console.log('there has been a connection with: ' + socket.id);
  
      socket.on('set-username', ({ roomId, username }) =&gt; {
        userService.setUsername(roomId, username, socket, io);
      });
  
      socket.on('start-game', ({ roomId, hostName }) =&gt; {
        roomService.startGame(roomId, hostName, socket, io);
      });
  
  });
  </code></pre>
  <p>roomService.test.js:</p>
  <pre><code>const chai = require('chai');
  const chaiHttp = require('chai-http');
  const server = require('../../server');
  const { expect } = chai;
  chai.use(chaiHttp);
  
  describe(&quot;example&quot;, () =&gt; {
    test('should run', () =&gt; {
      expect(true).to.be.true;
    })
  })
  </code></pre>