将 @testing-library/user-event 更新到 v.14 时出错

发布于 2025-01-18 06:38:40 字数 879 浏览 2 评论 0原文

我正在遵循最新版本的所有说明@testing-library/user-event

之前:

  test('request support action',() => {
    render(<SupportSection requestSupport={requestSupport} />);

    const button = screen.getByRole('button');

    userEvent.click(button);

    expect(requestSupport).toHaveBeenCalled();
  });

之后:

  test('request support action', async () => {
    const user = userEvent.setup();
    
    render(<SupportSection requestSupport={requestSupport} />);

    const button = screen.getByRole('button');

    await user.click(button);

    expect(requestSupport).toHaveBeenCalled();
  });

requestSupport在更高的范围const requestSupport = jest.fn();

我遇到的错误是:

typeerror:range.clonerange不是函数

我的所有测试都在库更新之前通过,现在都失败了。

I was following all the instructions for the newest version of @testing-library/user-event.

BEFORE:

  test('request support action',() => {
    render(<SupportSection requestSupport={requestSupport} />);

    const button = screen.getByRole('button');

    userEvent.click(button);

    expect(requestSupport).toHaveBeenCalled();
  });

AFTER:

  test('request support action', async () => {
    const user = userEvent.setup();
    
    render(<SupportSection requestSupport={requestSupport} />);

    const button = screen.getByRole('button');

    await user.click(button);

    expect(requestSupport).toHaveBeenCalled();
  });

requestSupport is mocked in the higher scope const requestSupport = jest.fn();

The error I'm getting is:

TypeError: range.cloneRange is not a function

All my tests were passing before the library updates, now all failing.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁绊已千年 2025-01-25 06:38:40

我终于找到了解决方案,由于一些旧模拟而发生了错误,这是先前的测试环境。所有必须完成的工作是从jest配置文件中删除下面的下面:

global.document.createRange = () => ({ ... });

I finally found the solution, the error occured due to some old mock, that previous test environment required. All that must be done, was to remove below from jest config file:

global.document.createRange = () => ({ ... });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文