All files / lib jsdom-setup.ts

0% Statements 0/59
100% Branches 1/1
100% Functions 1/1
0% Lines 0/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111                                                                                                                                                                                                                             
/* eslint-disable no-console */
import { JSDOM } from 'jsdom';
 
if (globalThis.process?.release?.name) {
  console.log('We are running in NodeJS');
  const dom = new JSDOM(`<!DOCTYPE html>`);
 
  globalThis.window = dom.window;
  globalThis.document = dom.window.document;
  globalThis.XMLSerializer = dom.window.XMLSerializer;
  globalThis.self = dom.window.self;
  if (!globalThis.navigator) {
    globalThis.navigator = dom.window.navigator;
  }
} else {
  console.log('Unknown runtime!');
}
 
if (globalThis.window) {
  const canvasCxtFunctText = window.HTMLCanvasElement.prototype.getContext.toString();
 
  if (canvasCxtFunctText.includes(' throw ')) {
    console.log('NOT real canvas');
    // This cannot be the real canvas, so let us mock out the 'getContext' funct
    // @ts-expect-error
    window.HTMLCanvasElement.prototype.getContext = () => {
      return {
        fillRect() {},
        clearRect() {},
        getImageData(x, y, w, h) {
          return {
            data: new Array(w * h * 4),
          };
        },
        putImageData() {},
        createImageData() {
          return [];
        },
        setTransform() {},
        drawImage() {},
        save() {},
        fillText() {},
        restore() {},
        beginPath() {},
        moveTo() {},
        lineTo() {},
        closePath() {},
        stroke() {},
        translate() {},
        scale() {},
        rotate() {},
        arc() {},
        fill() {},
        measureText() {
          return { width: 0 };
        },
        transform() {},
        rect() {},
        clip() {},
        bezierCurveTo() {},
      };
    };
 
    // jsdom's 'getContext' function looks like this (asOf jsdom v26):
    // getContext(contextId) {
    //     const esValue = this !== null && this !== undefined ? this : globalObject;
    //     if (!exports.is(esValue)) {
    //       throw new globalObject.TypeError(
    //         "'getContext' called on an object that is not a valid instance of HTMLCanvasElement."
    //       );
    //     }
 
    //     if (arguments.length < 1) {
    //       throw new globalObject.TypeError(
    //         `Failed to execute 'getContext' on 'HTMLCanvasElement': 1 argument required, but only ${arguments.length} present.`
    //       );
    //     }
    //     const args = [];
    //     {
    //       let curArg = arguments[0];
    //       curArg = conversions["DOMString"](curArg, {
    //         context: "Failed to execute 'getContext' on 'HTMLCanvasElement': parameter 1",
    //         globals: globalObject
    //       });
    //       args.push(curArg);
    //     }
    //     for (let i = 1; i < arguments.length; i++) {
    //       let curArg = arguments[i];
    //       curArg = conversions["any"](curArg, {
    //         context: "Failed to execute 'getContext' on 'HTMLCanvasElement': parameter " + (i + 1),
    //         globals: globalObject
    //       });
    //       args.push(curArg);
    //     }
    //     return utils.tryWrapperForImpl(esValue[implSymbol].getContext(...args));
    //   }
  } else {
    console.log('probably the real canvas, so no mocking');
    // This is probably the real canvas, so we do not need to do anything
    // The real 'getContext' should look something like this:
    //   function (contextType, contextAttributes) {
    //   if (contextType == '2d') {
    //     const ctx = this._context2d || (this._context2d = new Context2d(this, contextAttributes))
    //     this.context = ctx
    //     ctx.canvas = this
    //     return ctx
    //   }
    // }
  }
}