Jest: Jest does not call mocked static method

Created on 16 Jul 2019  路  1Comment  路  Source: facebook/jest

inb4: Already tried google, stack, and discord
Not sure if actually a bug, so would rather post it in here.

I have a JEST/Enzyme backed test setup for a component using an imported static method, that is used upon the elements click

const NextButton = (props: ISubmitButtonProps) => (
    <button
        onClick={Service.goToNext}
        className="col-2 submit"
        type="submit"
        disabled={props.submitting}>
        Next
    </button>
);

export default NextButton;

The method used is not async. It's just a simple _static_ method from the Service class.

In the test, I have set up a mock of the class to run a jest.fn() instead of the function itself. It's a method found here:

const goToNextMock = jest.fn();
jest.mock('Services/service', () => ({
        default: class {
            public static goToNext() {
                goToNextMock(); //the jest.fn from above
            }
        }
    })
);

And have added a test simulating the button click:

it('should call goToNext on button click', () => {
        //button is set before as shallow(<NextButton submitting={false}/>);
        button.simulate('click');
        expect(goToNext).toHaveBeenCalled();
    })

The problem is a solution like this results in the function not running:

Expected mock function to have been called.

The issue is not related to the simulate method (hence posting here, and not on Enzymes forum). Found that upon attaching a console log to the onClick that the simulate event works perfectly.

The mock itself also seems without issue. Know that from the _store related_ issues I would get if running the tests without it (it was calling for one specific variable that would be set in the store in a class used inside a class used inside the Service class

I have already tried running the simulate event with preventDefault(), and using the mock.bind... method with the same result

it('should call goToNextReport on button click', () => {
    const mock = jest.fn();
    Service.goToNext = mock.bind(Service);

    button.simulate('click', { preventDefault() {} });

    expect(mock).toHaveBeenCalled();
})

I'm a little new new to JEST and Enzyme, and do understand it's probably a quite basic error (10$ that it is). Great sorry to be coming here with it, but am slowly running out of options for its solution...well actually I have ran out of all the options. I have tried stack overflow and the JEST discord channel. I'm coming here because to put it simply: the issue is starting to drive me nuts and I have no idea what could be wrong with it.

Stackoverflow link, if anyone is more comfy in responding there

Here is a cool dophin photo for all you cool moderators that hopefully won't destroy this post the moment it emerges:
image

>All comments

The issue was answered on SO. Apparently when mocking static methods it is the best to just ignore that they are static...and methods

Was this page helpful?
0 / 5 - 0 ratings