gpt4 book ai didi

reactjs - Typescript Reactjs Jest/ enzyme 错误 - 'Type ' 任何 []' is missing the following properties...'

转载 作者:行者123 更新时间:2023-12-03 19:09:03 25 4
gpt4 key购买 nike

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();
});
但是对于这一行,我正在尝试模拟状态 -
useStateSpy.mockImplementation((init) => [init, setState]);
我收到错误:
Type 'any[]' is missing the following properties from type '[unknown, Dispatch<unknown>]': 0, 1ts(2739)
我不确定如何修复此错误,并且无法在网上找到很多与针对此特定用例修复此类错误相关的信息。

最佳答案

从我的角度来看,这有点像黑客,但你可以试试这个

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

关于reactjs - Typescript Reactjs Jest/ enzyme 错误 - 'Type ' 任何 []' is missing the following properties...',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62831750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com