与Waitfor一起使用的异步故事书测试

发布于 2025-02-04 19:35:18 字数 1811 浏览 2 评论 0原文

当我在本地运行test-Storybook时,以下测试通过,我的色度构建步骤通过,但是在Bitbucket管道中,此测试失败了。我想这与异步和waitfor有关。有人知道如何解决这个问题吗?

顺便说一句,使用async/等待getComputedStyle是首先使这些测试工作的唯一方法。 期望... tohavestyle不起作用。

const statuses = ['Open', 'In Progress', 'Cancelled', 'Closed'];
export const Default: PlayableStory = {
  args: {
    title: 'Filter by status',
    options: statuses.map(status => ({ value: status, label: status })),
  },
  play: async () => {
    const { getByText } = screen;
    statuses.forEach(status => expect(getByText(status)).toBeInTheDocument());

    const unSelectedBackgroundColor = 'rgba(0, 0, 0, 0)';
    const selectedBackgroundColor = 'rgba(0, 0, 0, 0.08)';

    async function expectChipSelected(text: string, selected = true) {
      await waitFor(() => {
        const chip = screen.getByRole('button', { name: text });
        expect(getComputedStyle(chip).backgroundColor).toEqual(
          selected ? selectedBackgroundColor : unSelectedBackgroundColor,
        );
      });
    }

    // select one
    await expectChipSelected(statuses[0], false);
    fireEvent.click(getByText(statuses[0]));
    await expectChipSelected(statuses[0]);
    
    // and more similar tests
  },
};

以及相关的管道步骤(我认为从某个地方复制了Collomatic的文档)

- step:
    name: 'Run storybook tests'
    caches:
      - node
    script:
      - npm install --legacy-peer-deps
      - npx playwright install --with-deps
      - npm run build-storybook --silent
      - npx concurrently -k -s first -n "SB,TEST" -c "magenta,blue" \
          "npx http-server storybook-static --port 6006 --silent" \
          "npx wait-on tcp:6006 && npm run test-storybook"

The following test passes when I run test-storybook locally, and my Chromatic build step passes, but in the Bitbucket pipeline, this test fails. I imagine it has to do with the async and waitFor. Does anyone know how to fix this?

By the way, using async/await and getComputedStyle was the only way to get these tests to work in the first place. expect...toHaveStyle didn't work.

const statuses = ['Open', 'In Progress', 'Cancelled', 'Closed'];
export const Default: PlayableStory = {
  args: {
    title: 'Filter by status',
    options: statuses.map(status => ({ value: status, label: status })),
  },
  play: async () => {
    const { getByText } = screen;
    statuses.forEach(status => expect(getByText(status)).toBeInTheDocument());

    const unSelectedBackgroundColor = 'rgba(0, 0, 0, 0)';
    const selectedBackgroundColor = 'rgba(0, 0, 0, 0.08)';

    async function expectChipSelected(text: string, selected = true) {
      await waitFor(() => {
        const chip = screen.getByRole('button', { name: text });
        expect(getComputedStyle(chip).backgroundColor).toEqual(
          selected ? selectedBackgroundColor : unSelectedBackgroundColor,
        );
      });
    }

    // select one
    await expectChipSelected(statuses[0], false);
    fireEvent.click(getByText(statuses[0]));
    await expectChipSelected(statuses[0]);
    
    // and more similar tests
  },
};

And the relevant pipeline step (copied from chromatic's docs somewhere, I think)

- step:
    name: 'Run storybook tests'
    caches:
      - node
    script:
      - npm install --legacy-peer-deps
      - npx playwright install --with-deps
      - npm run build-storybook --silent
      - npx concurrently -k -s first -n "SB,TEST" -c "magenta,blue" \
          "npx http-server storybook-static --port 6006 --silent" \
          "npx wait-on tcp:6006 && npm run test-storybook"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文