簡體   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