Stubbing of "watchPosition" in cypress

I try to inject position changes to an JS/REACT-Application. The Application registering at window.navigator.geolocation.watchPosition. My idea is to stub the "watchPosition" method to get a handle on the callback function. Then calling the callback function from the application directly.


    const watchPositionFake = (successCallback, errorCallback, options) => {
        console.debug("PROXY set callback watchPosition");
        originalWatchPositionSuccessCallback = successCallback;

    cy.visit("/", {
        onBeforeLoad(win) {
            cy.stub(win.navigator.geolocation, "watchPosition").callsFake(watchPositionFake);

This doesn’t work with function registering in the Application on the watchPosition. But this does work with function in the cypress-step file. (Working as in in the console.log I see changes in position according to the values I send in via originalWatchPositionSuccessCallback ).

Any idea who to fake a position change?

Source: Ask Javascript Questions