簡體   English   中英

TypeError:無法讀取未定義(讀取“長度”)JEST 測試的屬性

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM