I am writing the Unit test cases for my react project and using jest and enzyme for writing test cases. I have read the jest documentation
https://jestjs.io/docs/en/jest-object.html#jestspyonobject-methodname
which explains about jest.spyOn()
method but I didn't understand completely.
So I want to know more details about the specific places where we should use jest.fn()
and Where we should/must use jest.spyOn()
. It would be a great help if can be explained with an example for both methods.
Thanks
The jest. fn method allows us to create a new mock function directly. If you are mocking an object method, you can use jest.
Before every function is run in the file, jest will mock , and after every , jest will restore the function to its original implementation. Using the hooks for setup ensures that every test is fresh and independent of each other.
Example Usage of toHaveBeenCalled() When we write a unit test for funcB() , we would need to ensure funcA is called as well. Here is the example of the unit test. In the unit test below. We're using toHaveBeenCalled() to ensure funcA() was executed when we're calling funcB() .
With Jest's automatic mocks, we can mock classes or constructor functions easily. All methods are mocked with functions that return undefined . Then we can retrieve the mock by using mockedObject. mock.
My simple understanding of these two functions in react/frontend projects is the following:
jest.fn()
jest.fn()
)jest.spyOn()
mockRestore()
(if you just use a jest.spyOn()
without mocking it further it will still call the original function by default)(Good blog post: https://medium.com/@rickhanlonii/understanding-jest-mocks-f0046c68e53c)
To my understanding the only difference is that YOU CAN RESTORE ORIGINAL FUNCTION with jest.spyOn
and you can't with jest.fn
.
Imagine we have some hook that calls a function when component is rendered, here we can just check the function was called, we do not test that function.
Another case if we want original function to test how it works. And we need both in one test file.
Real method:
myMethod() {
return 33;
}
With jest.fn()
const myMethod = jest.fn().mockImplementation(() => 25);
const result = myMethod();
expect(result).toBe(25);
In case we want to test now real myMethod, we can't restore it back to normal with jest.fn().
Another thing with spies:
const spy_myMethod = jest.spyOn(component, "myMethod").mockImplementation(() => 25);
const result = myMethod();
expect(result).toBe(25);
And now if we want the original myMethod
spy_myMethod.mockRestore();
const result = myMethod();
expect(result).toBe(33);
jest.fn()
is a method to create a stub, it allowing you to track calls, define return values etc...
jest.spyOn()
came from jasmine, it allow you to convert an existing method on an object into a spy, that also allows you to track calls and re-define the original method implementation.
My rule of thumb on this is: if you want to make an existing implementation a spy use spyOn
if you are building a mock, use fn()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With