gpt4 book ai didi

reactjs - 如何修复 "Warning: useLayoutEffect does nothing on the server"?

转载 作者:行者123 更新时间:2023-12-03 13:21:28 43 4
gpt4 key购买 nike

完整错误如下:

Warning: useLayoutEffect does nothing on the server, because itseffect cannot be encoded into the server renderer's output format.This will lead to a mismatch between the initial, non-hydrated UI andthe intended UI. To avoid this, useLayoutEffect should only be used incomponents that render exclusively on the client

      in ForwardRef(ButtonBase)
in WithStyles(ForwardRef(ButtonBase))
in ForwardRef(Button)
in WithStyles(ForwardRef(Button))
in form
in div

每次运行测试时我都会得到它。这是我的测试

/* eslint-disable quotes */
import React from "react"
import { shallow, configure } from "enzyme"
import LoginForm from "../src/components/LoginForm"
import Button from "@material-ui/core/Button"
import Adapter from "enzyme-adapter-react-16"
import { render, fireEvent, cleanup } from "@testing-library/react"

configure({ adapter: new Adapter() })

describe("<LoginForm />", () => {
let wrapper
let usernameInput
let passwordInput
let signInButton

// Create initial props that get passed into the component
const initialProps = {
location: {
state: {
from: {
pathname: "/",
},
},
},
}

// Unit testing
describe("Unit tests", () => {
// what to do before each test
beforeEach(() => {
// Render the login form component, pass in props. (Shallow method renders the component without its children, good for unit tests.)
wrapper = shallow(<LoginForm {...initialProps} />)
usernameInput = wrapper.find("#username")
passwordInput = wrapper.find("#password")
signInButton = wrapper.find(Button)
})

// what to do after each test
afterEach(() => {
jest.clearAllMocks()
})

// UI Integrity test
it("should match the snapshot", () => {
// snapshots are text references of the html of the rendered component.
expect(wrapper.html()).toMatchSnapshot()
})

it("should have a username inputs", () => {
expect(usernameInput.length).toEqual(1)
})

it("should have the expected props on the username field", () => {
expect(usernameInput.props()).toEqual({
id: "username",
name: "username",
value: "",
type: "username",
onChange: expect.any(Function),
required: true,
})
})

it("should have a password field", () => {
expect(passwordInput.length).toEqual(1)
})

it("should have the expected props on the password field", () => {
expect(passwordInput.props()).toEqual({
id: "password",
name: "password",
value: "",
type: "password",
onChange: expect.any(Function),
required: true,
})
})

it("should have a submit button", () => {
expect(signInButton.length).toEqual(1)
})

it("should have the expected props on the button", () => {
expect(signInButton.props()).toEqual({
type: "button",
variant: "contained",
style: expect.objectContaining({
marginTop: "10px",
}),
onClick: expect.any(Function),
children: "Sign In",
})
})
})

// Integrations Testing
describe("Integrations tests", () => {
beforeEach(() => {
// Render the login form component, pass in props. (render method renders the component with its children, good for integrations tests, uses react-test-library.)
const { getByLabelText, getByText } = render(
<LoginForm {...initialProps} />
)
usernameInput = getByLabelText(/Username/i)
passwordInput = getByLabelText(/Password/i)
signInButton = getByText("Sign In")
})

afterEach(cleanup)

it("Username text change in onChange event", () => {
expect(usernameInput.value).toBe("")

fireEvent.change(usernameInput, { target: { value: "James" } })

expect(usernameInput.value).toBe("James")
})

it("Password text change in onChange event", () => {
expect(passwordInput.value).toBe("")

fireEvent.change(passwordInput, { target: { value: "mypassword" } })

expect(passwordInput.value).toBe("mypassword")
})

it("Test button submit", () => {
const mockLogin = jest.fn()

const button = shallow(<Button onClick={mockLogin} />)

button.simulate("click")

expect(mockLogin.mock.calls.length).toEqual(1)
})
})
})

我相信这与material-ui组件有关。我查了一下,here上有类似的问题这表明该问题与我的项目没有的依赖关系有关。所以我认为这与使用 useEffectLayout 的material-ui 组件有关,并且测试环境出于某种原因不喜欢这样。我使用yarn 和 jest yarn test 运行测试来运行测试套件。

最佳答案

添加

import React from "react" 
React.useLayoutEffect = React.useEffect

到我的 frontend/src/setupTests.js 测试文件是抑制 Jest 警告的一种方法。最终,这看起来是 Material-UI 组件与 Jest 存在问题的问题。

关于reactjs - 如何修复 "Warning: useLayoutEffect does nothing on the server"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58070996/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com