


我正在嘗試為 AGM 地圖撰寫測驗用例,但我發現了一個障礙,無法自行解決。幫助將不勝感激。請參閱螢屏截圖中的錯誤訊息。
uj5u.com熱心網友回復:
嘗試這樣的事情:
beforeEach(() => {
(window as any).navigator = mockNavigator;
});
const mockNavigator = {
geolocation: {
getCurrentPosition: () => {},
}
};
uj5u.com熱心網友回復:
**
Jest 無法讀取地理位置的型別。所以,我只是將型別添加為 Any 與全域變數。它對我來說非常有效。
**
it('should get current location', async () => {
const handleSpy = jest.spyOn(WhereToBuyComponent.prototype as any, 'setCurrentPosition');
const mockGeolocation = {
getCurrentPosition: jest.fn()
.mockImplementationOnce((success) => Promise.resolve(success({
coords: {
latitude: 51.1,
longitude: 45.3
}
})))
};
(global as any).navigator.geolocation = mockGeolocation;
expect(handleSpy).toBeCalled;
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/447568.html
