[Jest] Restore the Original Implementation of a Mocked JavaScript Function with jest.spyOn

We can use 'jest.spyOn', similr to 'spyOn' in Jasmine.

jest.spyOn(utils, 'getWinner')

We get 'getWinner' as a method.

 

Jest has mockImplementation:

// from

utils.getWinner = jest.fn((p1, p2) => p2)

// to

utils.getWinner.mockImplementation((p1, p2) => p2)

 

We can also do the cleanup after the test:

  // cleanup
  utils.getWinner.mockRestore()

 

posted @ 2020-04-30 15:01  Zhentiw  阅读(218)  评论(0)    收藏  举报