在react-18.0.0版本应用程序中,我正在为index.js文件编写测试用例,但出现以下错误
● 应用程序根目录 › 应该渲染而不会崩溃
TypeError: Cannot set property 'usingClientEntryPoint' of undefined
5 | import reportWebVitals from "./reportWebVitals";
6 |
> 7 | const root = ReactDOM.createRoot(document.getElementById("root"));
| ^
8 |
9 | root.render(
10 | <React.StrictMode>
和我的index.test.js
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
jest.mock("react-dom", () => ({ render: jest.fn() }));
describe("Application root", () => {
it("should render without crashing", () => {
const div = document.createElement("div");
div.id = "root";
document.body.appendChild(div);
require("./index.js");
expect(ReactDOM.render).toHaveBeenCalledWith(<App />, div);
});
});
如何解决这个问题。
当你嘲笑的时候反应-dom由于 createRoot 是从反应-dom/客户端,你需要这样做:
jest.mock("react-dom/client", () => ({
createRoot: jest.fn().mockImplementation(() => ({
render: jest.fn()
}))
}));
describe('Application root', () => {
it('should render without crashing', () => {
// render your app here
expect(renderFn).toHaveBeenCalled();
});
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)