0

I have a situation where I want to test a function that has been called in an if statement. I don't know how to test this function which actually returns a boolean.

Code

function test(){
  if(await SOP3loginConfig(props: object).isSOP3()){
    //calls if statements
  } else {
    //calls else statements
  }
}

In the above snippet, I am trying to test that function, I am able to do that, but can go through the if() branch.

I am using jest and react-testing-library.

I don't have access to the body of the functions within the if statements.

Tried this

it('Should call the SOP3 functions', () => {
      props.user = {};
      let SOP3loginConfig = (props: any) => {
        console.log(' ========================= I A M A TEST');
        return {
          isSOP3: () => {
            console.log(' ================ iSOP3 called');
            return true;
          },
        };
      };
      functions.start(props);
      expect(SOP3loginConfig(props).isSOP3()).toHaveBeenCalled();
      expect(props.history.push).not.toHaveBeenCalled();
    });

But got this error !

expect(received).toHaveBeenCalled()

    Matcher error: received value must be a mock or spy function

    Received has type:  boolean
    Received has value: true

      229 |       };
      230 |       functions.start(props);
    > 231 |       expect(SOP3loginConfig(props).isSOP3()).toHaveBeenCalled();
          |                                               ^
      232 |       expect(props.history.push).not.toHaveBeenCalled();
      233 |     });
      234 | 

1 Answer 1

1

Try using jest.fn

it('Should call the SOP3 functions', () => {
  props.user = {};
  const isSOP3Mock = jest.fn(() => {
    console.log(' ================ iSOP3 called');
    return true;
  })
  let SOP3loginConfig = (props: any) => {
    console.log(' ========================= I A M A TEST');
    return {
      isSOP3: isSOP3Mock,
    };
  };
  functions.start(props);
  expect(isSOP3Mock).toHaveBeenCalled();
  expect(props.history.push).not.toHaveBeenCalled();
});

assuming the functions.start(props) will call your test function.

Sign up to request clarification or add additional context in comments.

3 Comments

No Success ! ` expect(jest.fn()).toHaveBeenCalled() Expected number of calls: >= 1 Received number of calls: 0 230 | }; 231 | functions.start(props); > 232 | expect(isSOP3Mock).toHaveBeenCalled(); | ^ 233 | expect(props.history.push).not.toHaveBeenCalled(); 234 | }); 235 | `
What is expect(jest.fn()).toHaveBeenCalled() ? you do not need to call it in the expect. You call it before and in the expect check if the variable storing the function has been called.
Because I want to track whether the function is called or not, or maybe the condition is true or false !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.