Skip to content

How to mock an external package with deepMock #124

Open
@ofekisr

Description

I have a module, let's call it myModule that imports @myScope/myPackage
myPackage contains two functions - foo and goo

myModule exports the class 'MyClass' that uses both package functions

when I test MyClass I would like to mock the myPackage with mockDeep of jest-mock-extended
in each test, I would like to mock different return value or implementations of foo and goo

I didn't find a way to do so

// myModule.ts
import { foo, goo } from '@myScope/myPackage'

export class MyClass {
   toTest(): string {
      const a = foo().moo().koo() // that is why I want deepMock
      return goo(a + 'test')
   }
}

// myModule.test.ts
import { foo, goo } from '@myScope/myPackage'
import { MyClass } from './myModule'

describe('', () => {
  let myClass: MyClass

  beforeEach(() => {
    myClass = new MyClass()
  })

  it('should return an empty string', async () => {
    goo.mockReturnValue('')
    const result = myClass.toTest()
    expect(result).toEqual('')
  })
})

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions