【发布时间】:2021-06-05 12:56:23
【问题描述】:
我是单元测试的新手,我正在尝试渲染一个组件以了解有关该库的更多信息。
我正在尝试遵循 this 指南。
组件
<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]}}
对我做错了什么有什么建议吗?
【问题讨论】:
-
澄清一下,您在测试中使用的是
react-testing-library还是react-native-testing-library? -
嘿,我正在使用 react-native-testing-library :)
-
我使用了 testID,它按预期工作:)
标签: react-native jestjs react-native-testing-library jest-dom