Alternative to spyOn (). And.callfake in Jestjs

I used spyOn().and.callFakein jasmine, and it helps a lot in my tests, now I use Jest, I found in the document what jest.spyOn()exists, but without callFake.

My question is: how to spy on a method and call Fake with Jest and expect?

+4
source share
1 answer

The documentation page gives an excellent answer:

Note. By default, jest.spyOn also calls the spied method. This is different from most other test libraries. If you want to overwrite the original function, you can use jest.spyOn (object, methodName) .mockImplementation (() => customImplementation) or object [methodName] = jest.fn (() => customImplementation);

So, in your case, just pass the fake method customImplementation.

+2
source

Source: https://habr.com/ru/post/1676783/


All Articles