157

I want to mock a function with Jest, but only if it is called with specific arguments, for example:

function sum(x, y) {
  return x + y;
}
    
// mock sum(1, 1) to return 4
sum(1, 1) // returns 4 (mocked)
sum(1, 2) // returns 3 (not mocked)

There is a similar feature implemented in Ruby's RSpec library:

class Math
  def self.sum(x, y)
    return x + y
  end
end

allow(Math).to receive(:sum).with(1, 1).and_return(4)
Math.sum(1, 1) # returns 4 (mocked)
Math.sum(1, 2) # returns 3 (not mocked)

What I'm trying to achieve in my tests is a better decoupling, let's say I want to test a function that relies on sum:

function sum2(x) {
  return sum(x, 2);
}

// I don't want to depend on the sum implementation in my tests, 
// so I would like to mock sum(1, 2) to be "anything I want", 
// and so be able to test:

expect(sum2(1)).toBe("anything I want");

// If this test passes, I've the guarantee that sum2(x) is returning
// sum(x, 2), but I don't have to know what sum(x, 2) should return

I know that there is a way to implement this by doing something like:

sum = jest.fn(function (x, y) {
  if (x === 1 && y === 2) {
    return "anything I want";
  } else {
    return sum(x, y);
  }
});

expect(sum2(1)).toBe("anything I want");

But it would be nice if we had some sugar function to simplify it.

Does it sounds reasonable? Do we already have this feature in Jest?

Thanks for your feedback.

1
  • I've been dealing with this too, but in the end figured a very simple solution: const functionResult = myFunc(args); expect(functionResult).toBe('whatever');
    – paddotk
    Aug 26, 2021 at 16:08

6 Answers 6

125

I found this library that a colleague of mine wrote recently: jest-when

import { when } from 'jest-when';

const fn = jest.fn();
when(fn).calledWith(1).mockReturnValue('yay!');

const result = fn(1);
expect(result).toEqual('yay!');

Here's the library: https://github.com/timkindberg/jest-when

78

jest-when mentioned by STeve Shary is probably the best option.

If you don't want to install a new library, when you don't care about the original function, here's a one-line solution:

sum.mockImplementation((x, y) => x === 1 && y === 2 && "my-return-value")
7
  • 4
    Thanks for providing an alternative that doesn't require an external library.
    – theberzi
    May 11, 2022 at 13:43
  • 1
    If the order of the calls is known, it might be easier to instead chain .mockReturnValueOnce() multiple times.
    – theberzi
    May 11, 2022 at 13:45
  • 2
    But then you won't be able to set expectations on arguments
    – awvalenti
    May 12, 2022 at 15:41
  • This is mocking. It's assumed that the thing being mocked is not under test, otherwise it shouldn't be mocked at all.
    – theberzi
    May 13, 2022 at 6:58
  • 4
    It is part of the test to ensure that correct arguments are passed
    – awvalenti
    May 30, 2022 at 0:46
20
import * as helper from "../helper";  //file where all functions are

jest.spyOn(helper, "function_name").mockImplementation((argument) => {

// This argument is the one passed to the function you are mocking

  if (argument === "something") {
    return "something"
  } else {
    return "something else"
  }
});
0
19

I thought I needed a way to mock with arguments too, but for me, I could solve my problem by simply knowing the order of calls.

Taken from the jest documentation

const filterTestFn = jest.fn();

// Make the mock return `true` for the first call,
// and `false` for the second call
filterTestFn.mockReturnValueOnce(true).mockReturnValueOnce(false);

So in the example above, provided you know that on first pass the function should return true and on second pass it should return false, you're good to go!

6
  • 15
    I think this kind of tests are fragile because it depends on a very specific implementation. If the order of the calls aren't important in the implementation, I would not suggest with this approach. Dec 21, 2021 at 17:40
  • 2
    No, but this solution is for the scenario where the order of calls are important. Dec 21, 2021 at 18:00
  • 1
    OK. I just don't think the order of calls are important for OP's case so I wouldn't suggest this solution to OP. Dec 21, 2021 at 18:03
  • 4
    Not to the specific question no, but if you google "Can I mock functions with specific arguments using Jest" this question comes up and my answer is just merely pointing out that you may not need what you think you need. Dec 22, 2021 at 10:31
  • Yeah that's fair. Dec 23, 2021 at 17:59
12

No there is no way to do this in Jest yet. You could use sinons stubs for this. from the docs:

stub.withArgs(arg1[, arg2, ...]);

Stubs the method only for the provided arguments. This is useful to be more expressive in your assertions, where you can access the spy with the same call. It is also useful to create a stub that can act differently in response to different arguments.

"test should stub method differently based on arguments": function () {
    var callback = sinon.stub();
    callback.withArgs(42).returns(1);
    callback.withArgs(1).throws("TypeError");

    callback(); // No return value, no exception
    callback(42); // Returns 1
    callback(1); // Throws TypeError
}
2

This may help...

I had something similar whereby I had the same method called with different parameters requiring different returned result from the stubbed/mocked call. I've used a variable with a list of functions when I've made a call to the mocked service i take function off the top of the queue and execute function. It requires knowledge of the order of execution you are testing and doesn't really handle varying the response by argument but has allowed me to work around the restriction in jest.

var mockedQueue = [];
mockedQueue.push(() => {return 'A';})
mockedQueue.push(() => {return 'B';})

service.invoke = jest.fn(()=>{
    serviceFunctionToCall = mockedQueue.shift();
    return serviceFunctionToCall();
})
1
  • 10
    You can just use mockReturnValueOnce multiple times instead of this
    – Black
    Jun 4, 2020 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.