canvas.getContext("webgl") returns null when testing with puppeteer and jest

1.1k Views Asked by At

I am trying to write snapshot tests for a webgl renderer I've written using jest, puppeteer and jest-image-snapshot. The way I am testing this is I have written a react TestApp which contains the canvas as below:

export function TestApp(props: { width: number; height: number }): JSX.Element {
  return (
    <div
      id="drawing"
      style={{
        ...props
      }}
    >
      <DrawingCanvas canvasProps={props} renderBehavior={RenderBehavior.Wet} />
    </div>
  );
}

I need to provide my renderer the WebGLRenderingContext which it requires when rendering the scene. So before each test I do the following:

beforeEach(async () => {
    div = document.createElement("div");
    document.body.appendChild(div);
    ReactDOM.render(<TestApp width={800} height={600} />, div);
    const canvas: HTMLCanvasElement = document.getElementById("drawing-canvas-wet");
    webglRenderer= new WebGLRenderer(canvas.getContext("webgl"));
    testPlayground = new TestPlayground(webglRenderer);
  });

Whenever I call canvas.getContext("webgl") the WebGLRenderingContext returned is null. Has anyone had this problem before?

0

There are 0 best solutions below