title: Mock Es6 classes using Jest
body: <p>I'm trying to mock an ES6 class with a constructor that receives parameters, and then mock different class functions on the class to continue with testing, using Jest. </p>
  
  <p>Problem is I can't find any documents on how to approach this problem. I've already seen <a href="https://stackoverflow.com/questions/43749845/how-to-mock-es6-class-using-jest">this post</a>, but it doesn't resolve my problem, because the OP in fact didn't even need to mock the class! The other answer in that post also doesn't elaborate at all, doesn't point to any documentation online and will not lead to reproduceable knowledge, since it's just a block of code.</p>
  
  <p>So say I have the following class:</p>
  
  <pre><code>//socket.js;
  
  module.exports = class Socket extends EventEmitter {
  
      constructor(id, password) {
          super();
  
          this.id = id;
          this.password = password;
  
          this.state = constants.socket.INITIALIZING;
      }
  
      connect() {
          // Well this connects and so on...
      } 
  
  };
  
  //__tests__/socket.js
  
  jest.mock('./../socket');
  const Socket = require('./../socket');
  const socket = new Socket(1, 'password');
  
  expect(Socket).toHaveBeenCalledTimes(1);
  
  socket.connect()
  expect(Socket.mock.calls[0][1]).toBe(1);
  expect(Socket.mock.calls[0][2]).toBe('password');
  </code></pre>
  
  <p>As obvious, the way I'm trying to mock <strong>Socket</strong> and the class function <strong>connect</strong> on it is wrong, but I can't find the right way to do so.</p>
  
  <p>Please explain, in your answer, the logical steps you make to mock this and why each of them is necessary + provide external links to Jest official docs if possible!</p>
  
  <p>Thanks for the help!</p>