1

我正在尝试使用呈现 HTML5 画布的 React 组件进行图像快照测试(即,未模拟)。我正在使用 Jest、React 测试库、Node Canvas、Puppeteer 和 Jest Image Snapshot。

给定以下 React 组件render()

  public render(): React.ReactElement<TestCanvas> {
    const { innerWidth, innerHeight } = window;

    return (
      <div id="canvas" style={{ height: `${innerHeight}px`, width: `${innerWidth}px` }}>
        <canvas ref={this.canvasRef} />
      </div>
    );
  }

以下是 Jest 测试的样子:

  it('should render a <TestCanvas/> component', async () => {
    const { container } = render(<TestCanvas />);

    const page: puppeteer.Page = await browser.newPage();
    await page.setContent(container.outerHTML);
    const image: string = await page.screenshot();

    expect(image).toMatchImageSnapshot();
  });

但是,此测试会生成一个空白的白色 800x600 PNG 图像作为基线。

但是,如果我将测试更改为:

  it('should render a <TestCanvas/> component', async () => {
    const { container } = render(<TestCanvas />);

    const canvas: HTMLCanvasElement = container.querySelector('canvas') as HTMLCanvasElement;
    const img = document.createElement('img');
    img.src = canvas.toDataURL();

    const page: puppeteer.Page = await browser.newPage();
    await page.setContent(img.outerHTML);
    const image: string = await page.screenshot();

    expect(image).toMatchImageSnapshot();
  });

它基于我的 React 组件生成基线 PNG 快照就好了。

我目前正在尝试调试管道中出现问题的地方。

4

2 回答 2

2

我用不使用puppeteer的方法做了html5画布图像快照,但是puppeteer方法很有趣。这是我使用的方法

  test('canvas image snapshot', async () => {
    const { getByTestId } = render(
      <MyComponent />,
    )

    const canvas = await waitForElement(() =>
      getByTestId('mycanvas'),
    )

    const img = canvas.toDataURL()
    const data = img.replace(/^data:image\/\w+;base64,/, '')
    const buf = Buffer.from(data, 'base64')
    // may need to do fuzzy image comparison because, at least for me, on
    // travis-ci it was sometimes 2 pixel diff or more for font related stuff
    expect(buf).toMatchImageSnapshot({
      failureThreshold: 0.5,
      failureThresholdType: 'percent',
    })
  })
于 2020-02-28T04:50:59.717 回答
0

上面@Colin 的答案是人们需要做的。即,从画布 URL 中去除图像编码,因为 [显然] 它适用于浏览器。这是我们所做的:

it('should scroll down', () => {
  const { getByTestId } = render(<PaneDrawerTestWrapper />);

  const mouseHandler = getByTestId(mouseHandlerId);
  act(() => {
    fireEvent.wheel(mouseHandler, { deltaY: 100 });
  });

  const canvas = getByTestId(canvasId) as HTMLCanvasElement;
  const image = stripEncoding(canvas.toDataURL());
  expect(image).toMatchImageSnapshot();
});

哪里stripEncoding看起来像:

export function stripEncoding(canvasImageUrl: string): string {
  return canvasImageUrl.replace(/^data:image\/(png|jpg);base64,/, '');
}
于 2020-03-02T17:31:55.137 回答