title: Mock class prototype in javascript with jest
body: <p>I am currently on a project and i need to <code>mock</code> a certain <code>constructor</code> <code>class</code> <code>prototype</code> to run test so it doesn't deal directly with my <code>database</code>. I already tried researching and discovered i need to <code>import</code> the <code>module</code> into my <code>mock</code> file and then <code>export</code> it to my <code>test</code> file and use <code>jest.setMock()</code> to replace the module in my <code>test.js</code> file.  Below is the code to what i have tried.</p>
  
  <p>I have an <code>Admin</code> <code>constructor</code> <code>class</code> with a delete function in its <code>prototype</code> responsible for deleting from <code>db</code></p>
  
  <p><strong>admin.js</strong></p>
  
  <pre><code>function Admin(username, email, password, access) {
      Users.call(this, username, email, password, access);
  }
  
  Admin.constructor = Admin;
  
  Admin.prototype.deleteSingleUser = function(id, accountType) {
     this.id = id;
     this.accountType = accountType;
  
      let response = "";
  
      if(this.accountType === "admin") {
          for(let i in dbData.admin) {
              if(dbData.admin[i].id === this.id) {
                  dbData.admin.splice(i, 1);
                  fs.writeFileSync('db.json', JSON.stringify(dbData, null, 2));
                  console.log(dbData.admin);
                  response = "Account successfully deleted";
              }
              else {
                  response = "There is no user registered with this ID";
              }
          }
      }
      else if(this.accountType === "user") {
          for(let i in dbData.users) {
              if(dbData.users[i].id === this.id) {
                  dbData.users.splice(i, 1);
                  fs.writeFileSync('db.json', JSON.stringify(dbData, null, 2));
                  console.log(dbData.users);
                  response = "Account successfully deleted";
              }
              else {
                  response = "There is no user registered with this ID";
              }
          }
      }
      else {
          response = "Kindly use a valid account type";
      }
      console.log(response);
      return response;
  };
  </code></pre>
  
  <p>Then in my <code>__mocks__</code> directory i have an <code>admin.js</code> file containing a <code>mock</code> database and my <code>mock</code> delete code</p>
  
  <p><strong>__mocks__/admin.js</strong></p>
  
  <pre><code>//Imported the admin.js
  
  let Admin = require("../admin");
  
  //Created a mock database
  let mockDb = {
      "users": [
          {
              "id": 1,
              "username": "aaa",
              "email": "aaa@gmail.com",
              "password": "aaa",
              "access": "user"
          }
      ],
      "admin": [
          {
              "id": 1,
              "username": "bbb",
              "email": "bbb@gmail.com",
              "password": "bbb",
              "access": "admin"
          }
      ]
  } 
  
  //My mock delete function
  
  Admin.prototype.deleteSingleUser = function(id, accountType) {
      this.id = id;
      this.accountType = accountType;
  
      let response = "";
  
      if (this.accountType === "admin") {
          if(mockDb.admin[0].id === this.id) {
              response = "Account successfully deleted";
          }
          else {
              response = "There is no user registered with this ID";
          }
      }
      else if (this.accountType === "user") {
          if(mockDb.users[0].id === this.id) {
              response = "Account successfully deleted";
          }
          else {
              response = "There is no user registered with this ID";
          }
      }
      console.log("Its using the mock");
      return response;
  };
  module.exports = Admin;
  </code></pre>
  
  <p>Then in my <code>__test__</code> folder i have a <code>test.js</code> file with my test cases</p>
  
  <p><strong>__test__/test.js</strong></p>
  
  <pre><code>const Admin = require("../admin");
  jest.setMock("Admin", require("../__mocks__/admin"));
  
  test("Should check if admin can delete with a wrong id", () =&gt; {
      let response = Admin.prototype.deleteSingleUser(0, "user");
      expect(response).toBe("There is no user registered with this ID");
  });
  test("Should check if admin can delete a user", () =&gt; {
      let response = Admin.prototype.deleteSingleUser(1, "user");
      expect(response).toBe("Account successfully deleted");
  });
  </code></pre>
  
  <p>Neither of the test cases pass but both <code>throw</code> an <code>error</code> saying</p>
  
  <blockquote>
    <p>Test suite failed to run</p>
    
    <p>Cannot find module 'Admin' from 'test.js'</p>
    
    <p>2 | const Users = require("../main");</p>
    
    <p>3 | const Admin = require("../admin");</p>
    
    <p>4 | jest.setMock("Admin", require("../<strong>mocks</strong>/admin"));</p>
    
    <p>|      ^</p>
    
    <p>5 | const order = require("../order");</p>
    
    <p>6 | let mocks = require("../<strong>mocks</strong>/admin");</p>
    
    <p>7 | let dbData = JSON.parse(fs.readFileSync('db.json'));</p>
    
    <p>at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:229:17)</p>
    
    <p>at Object.setMock (<strong>test</strong>/test.js:4:6)</p>
  </blockquote>
  
  <p>I feel i am not doing something right but i can't seem to point it out. Please what is the issue and how do i fix it? Thanks.</p>