WebdriverIO: can’t access window attribute

I’m having trouble accessing the window attribute in WebdriverIO testing.

In the JavaScript code I create a new instance of OpenLayers:

import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';
import OSM from 'ol/source/OSM.js';

window.map = new Map({
  view: new View({
    center: [0, 0],
    zoom: 1,
  }),
  layers: [
    new TileLayer({
      source: new OSM(),
    }),
  ],
  target: 'map',
});

I want to use the instance in the WebdriverIO test.

So far I have tried the technique described in this answer, which is the following:

 const result = browser.execute(function() {
    return window['map']
 });

When I include the code above, it doesn’t execute.

What I want is the following:

describe('Mocha Example', () => {
    it('Test view', async () => {
        const view = window.map.getView()

        // do something with view
    })
})

The error we get is the following:

window is not defined

I think I should be able to access the window property directly from the describe -> it function because it’s not a Unit Test but it is End to End testing that uses a web browser to run the tests.

How can I access the map property of window?