๐Ÿ’ก This page contain affiliate links. By making a purchase through them, we earn a commission at no extra cost to you.
4 Ways to Mock Function Return Values in Jest

4 Ways to Mock Function Return Values in Jest

Ferenc Almasi โ€ข 2022 July 10 โ€ข ๐Ÿ“– 3 min read
  • twitter
  • facebook

Mocking functions is a core part of unit testing. Oftentimes, your original functions may have side effects that can break your test suite if not handled the right way. You can mock these functions to avoid any side effects, but sometimes you may only want to mock the return value of these functions.

To mock a function's return value in Jest, you first need to import all named exports from a module, then use mockReturnValue on the imported function.

// First, import all named exports from the module
import * as utils from 'utils'

utils.sum = jest.fn().mockReturnValue(2)

describe('Mock return value', () => {
    it('Should mock the return value of sum', () => {
        expect(utils.sum()).toBe(2)
    })
})
Copied to clipboard!

You can use the * as <alias> inside an import statement to import all named exports.

Then, you need to chain mockReturnValue off of jest.fn. In the above example, the return value of the sum function will be mocked to 2.

Looking to improve your skills? Master Jest from start to finish.
Master Jest

Mocking consecutive calls with different return values

In case you need to mock the return value of a function differently for each consecutive call, you can use a chain of mockReturnValueOnce.

it('Should mock the return value of consecutive calls differently', () => {
    utils.sum = jest.fn()
         .mockReturnValue(0)     // For default mocks
         .mockReturnValueOnce(2) // For the first call
         .mockReturnValueOnce(4) // For the second call

    expect(utils.sum()).toBe(2)
    expect(utils.sum()).toBe(4)
    expect(utils.sum()).toBe(0)
})
Copied to clipboard!

In the above example, the return value of the mocked function will be different for the first two calls. For the first call, it will return 2, for the second call, it will return 4, while for every other call, it will return 0. You can chain mockReturnValueOnce as many times as necessary, and create a default mocked value using mockReturnValue.


Mocking return values using mockImplementation

Another way to mock the return value of your function is using the mockImplementation call. Unlike mockReturnValue, this can also be used to mock the entire implementation of your functions, not just their return values.

utils.sum = jest.fn().mockImplementation(() => {
    // You can include your mock implementation here
    // Then mock the return value using a return statement
    return 2
})

// Or simply
utils.sum = jest.fn(() => {
    // You can also handle mock implementations this way
})
Copied to clipboard!

Note that you can also useย jest.fn(implementation)ย in place of mockImplementation. It is only a shorthand, therefore the functionality remains the same. Lastly, you can also use mockImplementationOnce to mock the return value differently for each consecutive call, just like with mockReturnValueOnce.

it('Should mock the return value of consecutive calls differently', () => {
    utils.sum = jest.fn()
         .mockImplementation(() => 0)     // For default mocks
         .mockImplementationOnce(() => 2) // For the first call
         .mockImplementationOnce(() => 4) // For the second call

    expect(utils.sum()).toBe(2)
    expect(utils.sum()).toBe(4)
    expect(utils.sum()).toBe(0)
})
Copied to clipboard!

This works in a very similar way to mockReturnValueOnce, except it also mocks the implementation of your function.

Did you find this page helpful?
๐Ÿ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Unlimited access to hundred of tutorials
  • check Access to exclusive interactive lessons
  • check Remove ads to learn without distractions
Become a Pro

Recommended

Ezoicreport this ad
All about Business & Industrial
visit carportmasters.com/ to learn more