πŸ’‘ This page contain affiliate links. By making a purchase through them, we may 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 β€’ Read time 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.

Copied to clipboard! Playground
// 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)
    })
})

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.


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.

Copied to clipboard! Playground
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)
})

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.

Copied to clipboard! Playground
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
})

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.

Copied to clipboard! Playground
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)
})

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

  • twitter
  • facebook
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.