首页 > 解决方案 > 测试 dom 库中的 waitFor 未按预期运行

问题描述

我正在使用测试 dom 库来测试 svelte 项目。在这里,当我向输入元素输入“com”时,它会调用端点,并且“COMAN”元素将作为端点的结果发生。所以我输入如下代码。

    let inputCom = document.getElementsByTagName("input")[0];
    await fireEvent.input(inputCom, { target: { value: "com" } });
    await waitFor(() => {
      expect(getByText("COMAN"));
    });

有时它会等待并且表现良好,但有时它会返回错误。我将回调函数更改为waitForasexpect(getByText("COMAN")).toBeVisible() 但我得到了相同的结果。即有时会得到我想要的结果,但有时不会。我认为 waitFor 函数的动作现在不稳定。

标签: unit-testingtestingasynctestsvelte-testing-library

解决方案


推荐阅读