react-native を使用すると、「受信した値は HTMLElement または SVGElement である必要があります」というエラーが表示されるのはなぜですか? 質問する

react-native を使用すると、「受信した値は HTMLElement または SVGElement である必要があります」というエラーが表示されるのはなぜですか? 質問する

私はユニットテストの初心者で、ライブラリについてさらに詳しく知るためにコンポーネントをレンダリングしようとしています。

私は従おうとしているこれガイド。

成分

<TouchableOpacity
    style={style}
    onPress={onPress}
    accessibilityRole="button"
>
    <AppText style={textStyle}>{title.toUpperCase()}</AppText>
</TouchableOpacity> 

テスト

it("Has the correct title in the button", () => {
    const { getByText } = render(<AppButton title="Hello" />);
  
    expect(getByText("HELLO")).toBeInTheDocument();
});

コンポーネントが正しくレンダリングされるかを確認しようとしているのですが、エラーが発生します

received value must be an HTMLElement or an SVGElement.
    Received has type:  object
    Received has value: {"_fiber": {"_debugHookTypes": null, "_debugID": 40, "_debugIsCurrentlyTiming": false, "_debugNeedsRemount": false, "_debugOwner": [FiberNode], "_debugSource": null, "actualDuration": 0, "actualStartTime": -1, "alternate": null, "child": [FiberNode], "childExpirationTime": 0, "dependencies": null, "effectTag": 1, "elementType": [Function Component], "expirationTime": 0, "firstEffect": null, "index": 0, "key": null, "lastEffect": null, "memoizedProps": [Object], "memoizedState": null, "mode": 0, "nextEffect": null, "pendingProps": [Object], "ref": null, "return": [FiberNode], "selfBaseDuration": 0, "sibling": null, "stateNode": [Component], "tag": 1, "treeBaseDuration": 0, "type": [Function Component], "updateQueue": [Object]}}

何が間違っているのかアドバイスをいただけますか?

ベストアンサー1

使用できますwaitFor

サンプル:

import { waitFor } from "@testing-library/react-native";


waitFor(() => expect(getByText("Your-text")).toBeInTheDocument());

// or

waitFor(() => expect(getByTestId("Your-Test-Id")).toBeInTheDocument());

おすすめ記事