Nícolas Iensen
Nícolas Iensen

Reputation: 4379

Can I mock functions with specific arguments using Jest?

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.

Upvotes: 169

Views: 202617

Answers (7)

Dan Murphy - Boulder
Dan Murphy - Boulder

Reputation: 181

You can do this in Jest, no external library required. Just keep a reference to the original implementation:

import * as MyModule from "./MyModule";

const originalMyFunction = MyModule.myFunction;

jest.spyOn(MyModule, "myFunction").mockImplementation((key) => {
 if (key === "keyIWantToMock") {
   return "someMockedValue";
 } else {
   return originalMyFunction(key);
 }
});

Upvotes: 0

peanutz
peanutz

Reputation: 516

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"
  }
});

Upvotes: 24

Luke Garrigan
Luke Garrigan

Reputation: 5011

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!

Upvotes: 18

awvalenti
awvalenti

Reputation: 2013

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")

Upvotes: 84

STeve Shary
STeve Shary

Reputation: 1505

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

Upvotes: 129

Adam
Adam

Reputation: 442

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();
})

Upvotes: 2

Andreas Köberle
Andreas Köberle

Reputation: 110912

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
}

Upvotes: 13

Related Questions