0

If I have a js file like:

  const doSomething = async (obj, time) => {
    // ...
  };

  const throttledDoSomething = (obj, time) => {
    const throttleTime = time + SOME_DELTA_VALUE;
    return throttle(() => doSomething(elem, time), throttleTime);
  };

How can i mock the call for doSomething when testing throttledDoSomething in jest?

I saw this question that answers it for jasmine, but I would like to stick to using jest. Also if anyone has tips on best practices in situations like that, it would be appreciated.

skyboyer
  • 22,209
  • 7
  • 57
  • 64
graciano
  • 310
  • 3
  • 10
  • `const mock = jest.fn((obj, time) => "bar");` then `expect(mock({a:b, 123)).toBe("bar")` – Randy Casburn May 19 '19 at 16:56
  • @RandyCasburn but this way it wouldn't test the function I need, but expecting a mocked value to be what was determined in the mock itself. – graciano May 19 '19 at 16:58
  • `expect(mock).toHaveBeenCalledWith({a:b, 123});` will verify the dependency is called correctly. That is the intent of a mock. Otherwise, you may be confusing the term _mock_ with the term _stub_. Do you actually want to test the `doSomething()` method from within a test case for the `throttleDoSomething()` method? – Randy Casburn May 19 '19 at 17:06
  • so you want to mock one function while testing another function from the same file, do I see it correct? – skyboyer May 19 '19 at 19:46
  • @skyboyer exactly – graciano May 19 '19 at 20:48
  • it was a duplicate indeed – graciano May 21 '19 at 20:48

0 Answers0