expected spyOn function to be called Jest

Viewed 1210

im currently trying to test the methods that i have created and the methods in my if statement are not being called. and im completely at a loss. I am new too jest though so im sure its something simple im missing.

 describe('isSingleScreen', () => {
    beforeEach(() => {
      jest.clearAllMocks();
      jest.spyOn(utilMethods, 'isDualScreen').mockReturnValue(true);
    });

    it('autoScreenAdd', () => {
      // Arrange
      const singleScreenAddSpy = jest.spyOn(
        singleScreenMethods,
        'singleScreenAdd'
      );
      const dualScreenAddSpy = jest.spyOn(dualScreenMethods, 'dualScreenAdd');

      // Act
      utilMethods.autoScreenAdd({});

      // Assert
      expect(singleScreenAddSpy).toBeCalledTimes(0);
      expect(dualScreenAddSpy).toBeCalled();
      expect(dualScreenAddSpy).toBeCalledTimes(1);
    });
  });

export const isDualScreen = (): boolean => {
  return Dimensions.get('window').width > 1000 ? true : false;
};

export const autoScreenAdd = (element: IDualComponent) => {
  if (isDualScreen()) {
    dualScreenAdd(element);
  } else {
    singleScreenAdd(element);
  }
};

this is the error i receive

    expect(jest.fn()).toBeCalledTimes(expected)

    Expected number of calls: 0
    Received number of calls: 1

      30 |       // Assert
      31 |       expect(autoScreenAddSpy).toBeCalled();
    > 32 |       expect(singleScreenAddSpy).toBeCalledTimes(0);
         |                                  ^
      33 |       expect(dualScreenAddSpy).toBeCalled();
      34 |       expect(dualScreenAddSpy).toBeCalledTimes(1);
      35 |     });

1 Answers

There is a limitation to how you can test a module that contains functions which call other functions within the same module. See this article for some more insight. There are few ways to work around this in that article, so I recommend taking a look at that first before diving into my rough implementation as it may not work with your code structure 100%.

Working Example:

CodesandBox

Utils

Slightly modified from your original, so you may need to mimic this in your util modules as necessary.

const isDualScreen = () => {
  return window.width > 1000 ? true : false;
};

const autoScreenAdd = element => {
  if (utilMethods.isDualScreen()) {
    utilMethods.dualScreenAdd(element);
  } else {
    utilMethods.singleScreenAdd(element);
  }
};

const dualScreenAdd = element => {
  return element;
};

const singleScreenAdd = element => {
  return element;
};

// This is important, it allows you to mock the functions properly in your tests.
// Use this same structure in your singleScreenMethods and dualScreenMethods modules
const utilMethods = {
  singleScreenAdd,
  dualScreenAdd,
  autoScreenAdd,
  isDualScreen
};

export default utilMethods;

Test Example

import utilMethods from "./utils";

describe("isSingleScreen", () => {
  beforeEach(() => {
    jest.clearAllMocks();
    jest.spyOn(utilMethods, "isDualScreen").mockReturnValue(true);
  });

  it("autoScreenAdd", () => {
    // Arrange
    const singleScreenAddSpy = jest.spyOn(utilMethods, "singleScreenAdd");
    const dualScreenAddSpy = jest.spyOn(utilMethods, "dualScreenAdd");

    // Act
    utilMethods.autoScreenAdd({});

    // Assert
    expect(singleScreenAddSpy).toHaveBeenCalledTimes(0);
    expect(dualScreenAddSpy).toHaveBeenCalledTimes(1);
  });
});
Related