为什么``userevent.click''不使用vitest和React测试库触发表格提交?
在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论