title: How to test function in class using jest
body: <p>I wasn't able to make unit testing worked using jest</p>
  
  <p>I'm trying to test a specific function that's calling or expecting result from other function but I'm not sure why it is not working. I'm pretty new to unit testing and really have no idea how could I make it work. currently this is what I've tried</p>
  
  <pre><code>export class OrganizationService {
  
      constructor() {
          this.OrganizationRepo = new OrganizationRepository()
      }
  
      async getOrganizations(req) {
          if (req.permission !== 'internal' &amp;&amp; req.isAuthInternal === false) {
              throw new Error('Unauthenticated')
          }
          const opt = { deleted: true }
          return this.OrganizationRepo.listAll(opt)
      }
  
  }
  </code></pre>
  
  <p>This is my OrganizationRepository that extends the MongoDbRepo</p>
  
  <pre><code>import { MongoDbRepo } from './mongodb_repository'
  
  export class OrganizationRepository extends MongoDbRepo {
  
      constructor(collection = 'organizations') {
          super(collection)
      }
  
  }
  </code></pre>
  
  <p>and this is the MongoDbRepo</p>
  
  <pre><code>const mongoClient = require('../config/mongo_db_connection')
  const mongoDb = require('mongodb')
  
  export class MongoDbRepo {
  
      constructor(collectionName) {
          this.collection = mongoClient.get().collection(collectionName)
      }
  
      listAll(opt) {
  
          return new Promise((resolve, reject) =&gt; {
              this.collection.find(opt).toArray((err, data) =&gt; {
                  if (err) {
                      reject(err)
                  }
                  resolve(data)
              })
          })
      }
  }
  </code></pre>
  
  <p>and this is the test that I've made</p>
  
  <pre><code>import { OrganizationService } from '../../../src/services/organization_service'
  
  describe('getOrganizations', () =&gt; {
      it('should return the list of organizations', () =&gt; {
          // const OrgRepo = new OrganizationRepository()
          const orgService = new OrganizationService()
  
          const OrgRepo = jest.fn().mockReturnValue("[{_id: '123', name: 'testname'}, {_id: '456, name: 'testname2'}]")
          // orgService.getOrganizations = jest.fn().mockReturnValue('')
          const result = orgService.getOrganizations()
  
          expect(result).toBe(OrgRepo)
      })
  })
  </code></pre>