Search code examples
typescriptunit-testingjestjsts-jest

restore mock of imported class function in ts-jest


Issue

I am mocking a function (only one) which is imported from another class which is being called inside the main class. I am able to mock the function and return the values which I provide. But I am not able to restore the mocked function back to normal for the subsequent tests.

Any help would be appreciated!

Framework Using: jest + ts-jest

Code

~main.ts

import {SubClass} from './subclass.ts'
export class MainClass {
 let sub: SubClass = new SubClass()

public async Car(){
  let start = await sub.key();
  return start
}

}

~sub.ts

export class SubClass{ 

public async key(){
  return "you can start the car"
}
}

main.test.ts

import {SubClass} from './subclass.ts'
import {MainClass} from './mainclass.ts'
import {mocked} from 'ts-jest/utils'

jest.mock(./subclass.ts)

let main = new MainClass()
let sub = new SubClass()
let mockedFn = mocked(sub,true)

mockedFn.key = jest.fn().mockImplementation(() => console.log('mocking succesfull'))

afterEach(()=>{
mockedFn.key.mockRestore() // tried mockClear(), mockReset()
}

it('test for main func mocked result',async ()=>{
 const result = await main.car()
 expect(result).toEqual("mocking succesfull")

}
it('test for main func result',async ()=>{
 const result = await main.car()
 expect(result).toEqual("you can start the car") // getting result as undefined
}



Solution

  • Original mockedFn.key method is lost when it's reassigned, there's no way how Jest could restore it:

    mockedFn.key = jest.fn()
    

    It should be:

    jest.spyOn(mockedFn, 'key').mockImplementation(...)
    

    If a method or property is supposed to be restored, it should never be spied/mocked by assigning jest.fn() directly. This especially applies to mocked globals that can cross-contaminate tests.