繁体   English   中英

使用 jest 和 History 时如何修复 TS 错误“缺少类型”?

[英]How to fix TS error 'type is missing' when working with jest and History?

我正在编写测试,并且正在使用模拟history object,但是 TS 不断抛出以下错误:

Type '{}' is missing the following properties from type 'Location<IHistoryMock>': pathname, search, state, hash

我不知道如何解决这个问题。 我尝试过扩展历史界面并以我能想到的各种方式重写它。 这是我到目前为止所拥有的:

import { History, Location } from 'history';

interface IHistoryMock extends History {
  location: Location | any;
}

const historyMock: History<IHistoryMock> = { push: jest.fn(), createHref: () => '', location: { }, listen: jest.fn() };

mount(
  <Router history={historyMock}>
    <Component id="id" />
  </Router>
);

Typescript 下划线location: { }并抛出上述错误。 我是否错误地扩展了History 我需要一个location是一个空的 object 这里,我不想使用any类型。

您可以在history库中使用createLocation来创建位置模拟

import { createLocation } from 'history';
...
const historyMock: History<IHistoryMock> = { push: jest.fn(), createHref: () => '', location: createLocation('/'), listen: jest.fn() };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM