title: Testing a function that returns an object with a function - JavaScript (Jest)
body: <p>I have a situation where I am facing an issue regarding unit testing of a function. There is a function <code>SOP3loginConfig</code> which returns an object, within that object we have a function <code>isSOP3</code> that returns a boolean, I want to test this function and cover the test part of it.</p>
  
  <p><strong>The actual implementation of the function <code>sop3login.ts</code></strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>export const SOP3loginConfig = (props: IVariables) =&gt; {
    const { i18n } = props;
    return {
      buttonLabel: props.user != null ? i18n('StartJourney') : i18n('logIn'),
      loginLink:"/login?redirectUrl="+window.location.href,
      isSOP3: async() =&gt; {
        let userData = await ADCServices.getUserInfo();
        if (!userData.session.tammUserInfo || userData.session.tammUserInfo.Type!="SOP3") {
          props.SOP3toggleModal(props,true);
          setTimeout(()=&gt;props.SOP3toggleModal(props,false),5000)
          return false;
        } else {
          return true;
        }
      },
    };
  };</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>My Integration <code>work.ts</code></strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import { SOP3loginConfig } from 'client/.../sop3login';
  
  const start = async (props: IVariables) =&gt; {
    if (props.user) {
      if (await SOP3loginConfig(props).isSOP3()) {
        props.history.push('/adc/card-renewal/customs');
      }
    } else {
      props.history.push(SOP3loginConfig(props).loginLink);
    }
  };</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Unit testing implmentation of mine <code>work.test.ts</code></strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  describe('Start SOP3loginConfig should be called', () =&gt; {
      it('Should call the SOP3', async () =&gt; {
        props.user = true;
        // const isSOP3Mock = () =&gt; {
        //   return true;
        // };
        // let SOP3loginConfig = async (props: any) =&gt; {
        //   return true;
        //   // return {
        //   //   isSOP3: isSOP3Mock,
        //   // };
        // };
        let SOP3loginConfig = jest.fn(props =&gt; {
          return {
            isSOP3: jest.fn(() =&gt; {
              return true;
            }),
          };
        });
        functions.start(props);
        expect(await SOP3loginConfig(props).isSOP3).toHaveBeenCalled();
        expect(props.history.push).toHaveBeenCalled();
      });
    });</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Error I am getting</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>    expect(jest.fn()).toHaveBeenCalled()
  
      Expected number of calls: &gt;= 1
      Received number of calls:    0
  
         97 |       });
         98 |       functions.start(props);
      &gt;  99 |       expect(await SOP3loginConfig(props).isSOP3).toHaveBeenCalled();
            |                                                   ^
        100 |       expect(props.history.push).toHaveBeenCalled();
        101 |     });
        102 |   });</code></pre>
  </div>
  </div>
  </p>
  
  <blockquote>
    <p>All I want to is cover the <code>if (await SOP3loginConfig(props).isSOP3())</code> part in <code>work.ts</code>.</p>
  </blockquote>