[英]TypeError: Cannot read properties of undefined (reading 'length') JEST test
提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文。
我正在尝试对我的项目实施笑话测试,但在运行测试时出现错误。 问题是我在尝试测试的代码中并没有真正的 length 方法。 它看起来像是出现在样式组件中。 有什么办法可以修复它,或者我可能解释错了?
测试
import React from "react";
import { render, screen } from "@testing-library/react";
import ChatBubblesContainer from "./ChatBubblesContainer";
let chats = [];
const mockUseContext = jest.fn().mockImplementation(() => ({
chats: [],
}));
React.useContext = mockUseContext;
describe("test", () => {
test("render", () => {
render(<ChatBubblesContainer />);
expect(screen.getByTestId("bubbles-container")).toBeInTheDocument();
});
});
错误
TypeError: Cannot read properties of undefined (reading 'length')
20 | describe("test", () => {
21 | test("render", () => {
> 22 | render(<ChatBubblesContainer />);
| ^
23 | expect(screen.getByTestId("bubbles-container")).toBeInTheDocument();
24 | });
25 | });
at charCodeAt (node_modules/styled-components/src/utils/hash.js:13:22)
at e.hash [as generateAndInjectStyles] (node_modules/styled-components/src/models/ComponentStyle.js:80:53)
at generateAndInjectStyles (node_modules/styled-components/src/models/StyledComponent.js:87:22)
at node_modules/styled-components/src/models/StyledComponent.js:129:30
at displayName (node_modules/styled-components/src/models/StyledComponent.js:247:14)
at renderWithHooks (node_modules/react-dom/cjs/react-dom.development.js:16305:18)
当开玩笑无法从特定方法中识别出某些特定方法时会出现这种情况使用此代码
jest.mock('PACKAGE NAME' , () => {
MISSING VARIABLE OR FUNCTION IN ERROR DESCRIPTION : () => jest.fn();
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.