React-Testing-Library/Jest-Dom - 接收的值必须是 HTMLElement 或 SVGElement

问题描述

我是单元测试的新手,我正在尝试渲染一个组件以了解有关该库的更多信息。

编辑:我正在尝试遵循 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]}}

感谢您对此进行调查!对我做错了什么有什么建议吗?

解决方法

这是有效的解决方案,如果我通过 testID 访问它就可以工作。

2021-03-08 09:28:05,871 TRACE [org.wildfly.security] (default task-6) Created HttpServerAuthenticationMechanism [org.wildfly.security.http.util.SecurityIdentityServerMechanismFactory$1@491e9ba0] for mechanism [FORM]
2021-03-08 09:28:05,871 TRACE [org.wildfly.security.http.form] (default task-6) Trying to re-authenticate. There is no session attached to the following request. Request URI: [http://localhost:8080/login/],Context path: [/]
2021-03-08 09:28:05,871 TRACE [org.wildfly.security] (default task-6) Handling CachedIdentityAuthorizeCallback: principal = null  authorizedIdentity = null

虽然理解为什么我无法通过 cpuid 获得价值会很好,但至少它是有效的。 :)