Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expect one function to call another function?

I am trying to mock a function call, and expect it to have called another function within it once.

myFunctions.test.js

import { resetModal } from '../myFunctions.js';  describe('resetModal', () => {   it('calls the clearSomethingInModal function', () => {     const clearSomethingInModal = jest.fn();     resetModal();     expect(clearSomethingInModal.mock.calls.length).toBe(1);   }) }) 

myFunctions.js

export resetModal() {   clearSomethingInModal() } 

However, Jest output says that it has not been called. How can I best do this?

like image 973
matt-p Avatar asked Nov 23 '16 18:11

matt-p


People also ask

How do you call one function from another function?

To call a function inside another function, define the inner function inside the outer function and invoke it. When using the function keyword, the function gets hoisted to the top of the scope and can be called from anywhere inside of the outer function.

How do you mock a function and expect it to be called?

You can create a namespace that you export as the default object and call b using the namespace. This way, when you call jest. mock it will replace the b function on the namespace object. const f = require('./f'); jest.

Can you call another function within a function?

Calling a function from within itself is called recursion and the simple answer is, yes.

How do you call a mock function?

You can mock functions in two ways: either you create a mock function to use in test code, or you write a manual mock that overrides a module dependency. Let's take for example the case where we're testing an implementation of a function forEach, which will invoke a callback for each item in a supplied array.


2 Answers

Your approach does not work because you mock clearSomethingInModal only in the context of your test file, so clearSomethingInModal in the myFunctions.js is still the original. The main point is that you can't mock something that is directly created in myFunctions.js. The only thing that you can mock are:

  1. Modules that you import to myFunctions.js, like import clearSomethingInModal from 'clearSomethingInModal';
  2. Callbacks that you pass into your function when calling them from your test;

This makes sense if you think about myFunctions.js as a blackbox, where you can control what goes in, like imports or function arguments, and where you can test what comes out. But you can not test the stuff that happens inside the box.

Here are two example that reflect the 2 points in the list:

myFunctions.test.js

import { resetModal } from '../myFunctions.js'; import clearSomethingInModal from 'clearSomethingInModal';  jest.mock('clearSomethingInModal', () => jest.fn())  describe('resetModal', () => {   it('calls the clearSomethingInModal function', () => {     resetCreationModal();     expect(clearSomethingInModal.mock.calls.length).toBe(1);   }) }) 

myFunctions.js

import clearSomethingInModal from 'clearSomethingInModal';  export resetModal() {   clearSomethingInModal() } 

myFunctions.test.js

import { resetModal } from '../myFunctions.js';  describe('resetModal', () => {   it('calls the clearSomethingInModal function', () => {     const clearSomethingInModal = jest.fn();     resetCreationModal(clearSomethingInModal);     expect(clearSomethingInModal.mock.calls.length).toBe(1);   }) }) 

myFunctions.js

export resetModal(clearSomethingInModal) {   clearSomethingInModal() } 
like image 153
Andreas Köberle Avatar answered Sep 18 '22 21:09

Andreas Köberle


Another way is to use done and mock or spy on the implementation of the last function and check if the previous function was called by then.

it('should call function2 after function1', (done) => {     expect.assertions(2)      function2.mockImplementationOnce(() => {       expect(function1).toHaveBeenCalled()       done()     })      act() // This is where you run function you are testing   }) 

The drawback of this solution is that the structure of the test is not

// arrange // act // assert 

but rather

// arrange & assert // act 
like image 33
オスカー Avatar answered Sep 21 '22 21:09

オスカー