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

标签 reactjs typescript jestjs enzyme

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/

相关文章:

reactjs - 如何在下次按下时聚焦下一个输入(如果有)

TypeScript 根据输入类型定义通用输出类型

javascript - 无法在 TypeScript 中设置未定义的属性 XXX

typescript - ES6/ typescript 导入 : import * and names on a single line

javascript - 如何使用 jest 测试 javascript 中的抽象函数?

react-native - 语法错误 : Missing initializer in const declaration

reactjs - React 中的登录 session

javascript - 条件渲染 react 问题

javascript - ReactJs:仅返回数组中的唯一值

node.js - 使用 vue.js 提高 JEST 测试性能