[英]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.