为什么``userevent.click''不使用vitest和React测试库触发表格提交?

发布于 2025-02-09 07:21:28 字数 1501 浏览 0 评论 0原文

在使用Create Appl时,以下测试可以运行并通过框即可。

import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import App from "./App";

describe("Update the name", () => {
  it("updates the name", () => {
    render(<App />);
    userEvent.type(screen.getByLabelText(/name/i), "John");
    userEvent.click(screen.getByRole("button"));

    expect(screen.getByText("John")).toBeInTheDocument();
  });
});

因此,测试本身应该很好。是的,只有1️⃣按钮,即type =“ submit”。在浏览器中运行时,APP在Create App App(CRA)中运行时可以正常工作。

我正在尝试使用Vitest(Vite)。从理论上讲,上述测试不需要任何更改。但是,这就是发生的情况:typeError:target.ownerdocument.Createange不是函数

让我们来到配置。

vite.config.js看起来像这样:

/// <reference types="vitest" />
/// <reference types="vite/client" />

import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,
    environment: "happy-dom",
    setupFiles: "./src/setup.js",
  },
});

./ src/setup.js看起来像这样:导入“ happy-dom”;

是,我还尝试了jsdom

我还用fireevent.click替换userevent.click。我还将data-Testid添加到我的表单中,并在该表单上直接尝试了fireevent.submit

在后一种情况下,没有错误,但是表格提交未触发,或者提交提交是被触发的,但渲染尚未更新。

事实是,如果

The following test runs and passes just fine when using Create React App out of the box.

import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import App from "./App";

describe("Update the name", () => {
  it("updates the name", () => {
    render(<App />);
    userEvent.type(screen.getByLabelText(/name/i), "John");
    userEvent.click(screen.getByRole("button"));

    expect(screen.getByText("John")).toBeInTheDocument();
  });
});

So, the test itself should be just fine. Yes, there is only 1️⃣ button, and that is type="submit". App works when running in browser and tests are fine in Create React App (CRA).

I am trying to move to Vitest (with Vite). Theoretically, no changes should be necessary to the above test. However, here's what happens: TypeError: target.ownerDocument.createRange is not a function.

Let's get to the configurations.

vite.config.js looks ???? like this:

/// <reference types="vitest" />
/// <reference types="vite/client" />

import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,
    environment: "happy-dom",
    setupFiles: "./src/setup.js",
  },
});

./src/setup.js looks like this: import "happy-dom";

Yes, I also tried with jsdom.

I also replaced userEvent.click with fireEvent.click. I also added a data-testid to my form and tried fireEvent.submit directly on that form.

In the latter scenarios, no errors but either the form submit wasn't triggered, or the submission was triggered, but the render was not updated.

Fact of the matter is if the ???? works in CRA, then the test should not have to be changed. Possibly only the configurations. ????

I love Vite, but can't continue with it if I can't get the testing ✅ experience working.

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

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

发布评论

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