function overrideCreateElement(window) {
  const d = window.document;
  const f = window.document.createElement;

  // Check if 2D context already present. That happens either when running in a browser,
  // or this is node.js with 'canvas' installed.
  const ctx = d.createElement("canvas").getContext("2d");
  if (ctx !== null && ctx !== undefined) {
    //console.log('2D context is present, no need to override');
    return;
  }

  window.document.createElement = function (param) {
    if (param === "canvas") {
      if (canvasMock === undefined) {
        canvasMock = f.call(d, "canvas");
        replaceCanvasContext(canvasMock);
      }
      return canvasMock;
    } else {
      return f.call(d, param);
    }
  };
}
Overrides document.createElement(), in order to supply a custom canvas element.

In the canvas element, getContext() is overridden in order to supply a simple
mock object for the 2D context. For all other elements, the call functions unchanged.

The override is only done if there is no 2D context already present.
This allows for normal running in a browser, and for node.js the usage of 'canvas'.
@param {object} window - current global window object. This can possibly come from module 'jsdom',
                when running under node.js.
@private