Typescript Reactjs Jest/Enzyme Error - 'Type 'any[]' is missing the following properties...'

Viewed 956
let wrapper;
const setState = jest.fn();
const useStateSpy = jest.spyOn(React, "useState")
useStateSpy.mockImplementation((init) => [init, setState]);

beforeEach(() => {
    wrapper = Enzyme.mount(Enzyme.shallow(<AddOption/>).get(0))
});

afterEach(() => {
    jest.clearAllMocks();
});

I am trying to mock state however for this line -

useStateSpy.mockImplementation((init) => [init, setState]);

I am getting the error:

Type 'any[]' is missing the following properties from type '[unknown, Dispatch<unknown>]': 0, 1ts(2739)

I am unsure as to how to fix this error and haven't been able to find much online in relation to fixing an error like this for this particular use case.

1 Answers

From my perspective it is kind of a hack but you can try this

...
const init = {}; // or anything you want to be the initial state
useStateSpy.mockImplementation(() => [init, setState] as any);
...

OR

...
const init = {}; // or anything you want to be the initial state
useStateSpy.mockReturnValue([init, setState] as any);
...
Related