【问题标题】:Typescript Reactjs Jest/Enzyme Error - 'Type 'any[]' is missing the following properties...'Typescript Reactjs Jest/Enzyme 错误 - '类型'任何 []' 缺少以下属性...'
【发布时间】:2020-10-31 01:05:33
【问题描述】:
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)

我不确定如何修复此错误,也无法在网上找到很多关于针对此特定用例修复此类错误的信息。

【问题讨论】:

  • 为什么要同时使用mount和shallow来渲染AddOption?
  • @Rostyslav 我正在按照在线指南为表单组件编写测试,所以老实说我不太确定......我是反应测试的新手,但网上的很多示例都没有使用打字稿另外,就像我一样 - dev.to/austinbh/…

标签: reactjs typescript jestjs enzyme


【解决方案1】:

从我的角度来看,这是一种 hack,但你可以试试这个

...
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);
...

【讨论】:

  • 给出错误 - '(init: any) => any[]' 类型的参数不可分配给 '() => [unknown, Dispatch]' 类型的参数
  • @monkeys73 应该有指示错误发生的行。请您提供该行的测试代码吗?还有 AddOption?
  • @monkeys73 乐于提供帮助 :) 顺便说一句,如果您觉得我的回答有用,请接受并投赞成票,这样问题就可以被视为已解决
猜你喜欢
  • 2021-01-05
  • 2021-06-24
  • 2020-08-14
  • 2017-11-10
  • 2019-08-25
  • 2020-05-23
  • 1970-01-01
  • 2021-03-24
  • 2020-04-07
相关资源
最近更新 更多