How to test if function was called with defined parameters ( toHaveBeenCalledWith ) with Jest : <p>I want to test, if particular function was called in my test and with the correct parameters. From JEST documentation I'm not able to figure out, what is the correct way to do it.</p>

<p>Let's say I have something like this:</p>

<pre><code>// add.js

function child(ch) {
   const t = ch + 1;
   // no return value here. Function has some other "side effect"
}


function main(a) {
  if (a == 2) {
    child(a + 2);
  }

  return a + 1;
}

exports.main = main;
exports.child = child;
</code></pre>

<p>Now in unit test:</p>

<p>1.
I want to run <code>main(1)</code> and test that it returned <code>2</code> and <code>child()</code> was not called.</p>

<p>2.
And then I want to run <code>main(2)</code> and thest that it returned <code>3</code> and <code>child(4)</code> was called exactly once.</p>

<p>I have something like this now:</p>

<pre><code>// add-spec.js
module = require('./add');

describe('main', () =&gt; {

  it('should add one and not call child Fn', () =&gt; {
    expect(module.main(1)).toBe(2);
    // TODO: child() was not called
  });

  it('should add one andcall child Fn', () =&gt; {

    expect(module.main(2)).toBe(3);
    // TODO: child() was called with param 4 exactly once
    // expect(module.child).toHaveBeenCalledWith(4);
  });

});
</code></pre>

<p>I'm testing this in <a href="https://repl.it/languages/jest" rel="noreferrer">https://repl.it/languages/jest</a> , so a working example in this REPL will be much appreciated.  </p>
