簡體   English   中英

React App - 使用 Jest 和 Enzyme 進行測試時無法讀取孩子的道具(變得未定義)

[英]React App - cannot read prop of children (getting undefined) while testing using Jest and Enzyme

我正在嘗試使用 Jest 和 Enzyme 對 React 應用程序進行第一次測試。 我有一個組件 SignUpForm ,它接受孩子並根據其當前狀態值呈現正確的。

例子:

if (children.length < currentStep) {
      return <Success />;
    }

一切正常,除非我嘗試對其進行測試。 這是因為我指向兒童道具並說:

類型錯誤:無法讀取未定義的屬性“長度”

這就是我現在在測試中所做的:

it("Should render without crashing", () => {
    const component = shallow(<SignUpForm />);
    console.log(component);
  });

您想測試根本沒有孩子或未定義的場景,這很好。 您需要做的就是再添加一項檢查children變量是否存在:)

if (!children || children.length < currentStep) {
      return <Success />;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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