Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if function was called with defined parameters ( toHaveBeenCalledWith ) with Jest

Tags:

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.

Let's say I have something like this:

// 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; 

Now in unit test:

1. I want to run main(1) and test that it returned 2 and child() was not called.

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

I have something like this now:

// add-spec.js module = require('./add');  describe('main', () => {    it('should add one and not call child Fn', () => {     expect(module.main(1)).toBe(2);     // TODO: child() was not called   });    it('should add one andcall child Fn', () => {      expect(module.main(2)).toBe(3);     // TODO: child() was called with param 4 exactly once     // expect(module.child).toHaveBeenCalledWith(4);   });  }); 

I'm testing this in https://repl.it/languages/jest , so a working example in this REPL will be much appreciated.

like image 211
DHlavaty Avatar asked Jul 07 '17 12:07

DHlavaty


People also ask

How do you test if a function has been called in Jest?

To check if a component's method is called, we can use the jest. spyOn method to check if it's called. We check if the onclick method is called if we get the p element and call it.

What is toHaveBeenCalledWith in Jest?

Matching on arguments in function calls with Jest's toHaveBeenCalledWith. We use toHaveBeenCalledWith when we want to assert that a function was called with a specific set of arguments. If you only care about a specific argument in a function call, you can replace the other arguments with expect. anything() .

Does Jest support parameterized tests?

Jest has a built-in support for tests parameterized with data table that can be provided either by an array of arrays or as tagged template literal.

Which assertion used to test a value is with exact equality in Jest?

Common Matchers​ The simplest way to test a value is with exact equality. expect(2 + 2).


1 Answers

OK, I've figured it out. The trick is, to split functions into separate files. So the code is (and works in https://repl.it/languages/jest ):

// add.js child = require('./child').child;  function main(a) {   if (a == 2) {     child(a + 2);   }    return a + 1; }  exports.main = main; 

extracted child.js file:

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

main test file:

// add-spec.js main = require('./add').main; child = require('./child').child;  child = jest.fn();  describe('main', () => {    it('should add one and not call child Fn', () => {     expect(main(1)).toBe(2);      expect(child).toHaveBeenCalledTimes(0);   });    it('should add one andcall child Fn', () => {     expect(main(2)).toBe(3);      expect(child).toHaveBeenCalledWith(4);     expect(child).toHaveBeenCalledTimes(1);   });  }); 
like image 90
DHlavaty Avatar answered Sep 19 '22 12:09

DHlavaty