1

So I have a module called A with two functions:

function handleCreatedOrder(){
  console.log("here")
}

function acquireOrder(){
  if(condition) handleCreatedOrder
}

I have a test which needs to check if handleCreatedOrder is getting called. I have mocked the module by first importing the module import * as acquireOrderWrapper from './module'

Then in the test I am spying on the module by let handleCreatedOrderSpy = jest.spyOn(acquireOrderWrapper, "handleCreatedOrder")

When I try to assert expect(handleCreatedOrder).toBeCalled() it fails because the received number of calls does not match. I understand this is because the mocking is not proper as the console.log from handleCreatedOrder is called but the expect statement fails. What am I doing wrong?

My tests are currently like

describe("handle acquireOrder success responses", () => {
  const reactRedux = { useDispatch, useSelector }
  const useDispatchMock = jest.spyOn(reactRedux, "useDispatch")

  test("test1", async () => {
    let handleCreatedOrderSpy = jest.spyOn(acquireOrderWrapper, "handleCreatedOrder")
    await acquireOrder()
    expect(handleCreatedOrderSpy).toBeCalled()
  }
  • Could you provide the full code for your test ? Do you mock your module at the top of the file ? Or in a describe ? – Florian Motteau Sep 26 '22 at 14:28
  • I cant mock the module because it then overrides acquireOrder() – Leo Baby Jacob Sep 26 '22 at 14:47
  • The way you're doing it, it should only mock `handleCreatedOrder`, other functions in module should not be mocked. What I meant was, the `jest.spyOn...` line should be at the top of the file, before `describe` – Florian Motteau Sep 26 '22 at 14:50
  • I have added the test the way it is right now. I also did try out https://stackoverflow.com/a/64401168/16300815 i.e requireActual and added handleCreateOrder to a jest.fn(). It still fails. – Leo Baby Jacob Sep 26 '22 at 14:59

1 Answers1

0

I think what you need to do is create a spy on the handleCreatedOrder function first and then call the acquireOrder function inside your test.

The spy will then be called once you execute the acquireOrder function and your test should pass. I usually develop my frontend with angular and typescript but for reactJS it should look similiar.

test('test if function was called', async () => {
  jest.spyOn(component, 'handleCreatedOrder').mockImplementation();
  await acquireOrder();
  expect(component.handleCreatedOrder).toHaveBeenCalled();
}

you might need to add a changeDetector in your test after running the acquireOrder function. In typescript ususally i add fixture.detectChanges after running the function like this

fixture.detectChanges();

Also i noticed you are missing () after your function.

function acquireOrder(){
  if(condition) handleCreatedOrder()
}
Fiehra
  • 659
  • 6
  • 23
  • I actually did have a spy for handlecreatedOrder in the question. It is a react app but I am trying to test out a function. It would not call the mocked function instead calls the original handlecreatedOrder() – Leo Baby Jacob Sep 26 '22 at 15:04
  • mhm yes i saw your updated question. btw i noticed in your file you are using handleCreatedOrder instead of handleCreatedOrder() – Fiehra Sep 26 '22 at 15:07
  • 1
    I think fixture detect changes is more angular than react. Also yes I had passed the parenthesis in the actual function, so that should not be a problem – Leo Baby Jacob Sep 26 '22 at 15:14
  • mhm honestly your test logic looks fine to me. it should work. you could try to move the test outside the describe block but idk that shouldn matter – Fiehra Sep 26 '22 at 17:32
  • I think that the issue is simillar to https://stackoverflow.com/a/62587700/16300815 but I am not sure how to replace the cjs exports here. – Leo Baby Jacob Sep 26 '22 at 17:36