无法分配给 'geolocation',因为它是只读的 属性(angular 笑话)

Cannot assign to 'geolocation' because it is a read-only property (angular Jest)

我正在尝试为 AGM 地图编写测试用例,但我发现了一个障碍,无法自行解决。帮助将不胜感激。 请参考屏幕截图中的错误消息。

尝试这样的事情:

beforeEach(() => {
  (window as any).navigator = mockNavigator;    
});

const mockNavigator = {
  geolocation: {
    getCurrentPosition: () => {},
  }
};

**

Jest is not able a read the type of geolocation. So, I just added the type as Any with the global variable. It worked perfectly for me.

**

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