Cypress modyfing window object in test results in cy.window() yielding undefined in next test

I have test suite in cypress.js where in before each test I delete specific database from indexedDB:

beforeEach(() => {
   cy.window().then(win => win.indexedDB.deleteDatabase('test_db'));
});

In each test I use custom command which stubs specific properties on window object:

Cypress.Commands.add('stubDevice', () => {
  Cypress.on('window:before:load', win => {
    // @ts-expect-error TS does not recognize userAgentData global variable
    const userAgentData = { ...win.navigator.userAgentData };
    const mockedUserAgentData = {
      ...userAgentData,
      mobile: true,
    }

    Object.defineProperty(win.navigator, 'userAgentData', {
      value: mockedUserAgentData,
      configurable: true,
    });
  });
});

When test suite is run, first test works fine, however beforeEach callback before second test throws error Cannot read properties of undefined (reading 'indexedDB'). Looks like cy.window() yields undefined and it seems that using defineProperty on win.navigator is a problem, because when I comment out that part of custom command, beforeEach callback works fine before every test. Why that happens and how can I prevent that?