Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Testing-Library/Jest-Dom - Received value must be an HTMLElement or an SVGElement

I'm new to unit testing and I'm trying to render a component to learn more about the library.

I'm trying to follow this guide.

Component

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

Test

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

I'm simply trying to see that the component renders correctly but I get the error

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]}}

Any advice on what I'm doing wrong?

like image 205
theabrar Avatar asked Mar 07 '21 12:03

theabrar


People also ask

What is the react testing library?

The React Testing Library is a very light-weight solution for testing React components. It provides light utility functions on top of react-dom and react-dom/test-utils, in a way that encourages better testing practices. Its primary guiding principle is:

What is the use of jest in react testing?

Jest is a test runner that finds tests, runs the tests, and determines whether the tests passed or failed. Additionally, Jest offers functions for test suites, test cases, and assertions. React Testing Library provides virtual DOMs for testing React components.

What is the difference between @testing-library/user-event and @jest-Dom?

jest-dom isn’t required to use React Testing Library but it makes tests more readable. @testing-library/user-event isn’t required, too, but makes testing user interaction closer to how an actual user would interact with the app.

What is the best framework for testing react apps?

The React community recommends Jest as the React testing framework of choice. Jest is used by many high-profile companies, such as Facebook, Uber, and Airbnb. Jest also happens to be the default for many JavaScript frameworks out of the box including create-react-app.


2 Answers

The issue is that you're trying to use the toBeInTheDocument helper function from @testing-library/jest-dom with React Native code. @testing-library/jest-dom expects DOM elements to be passed to its functions, and is meant to be used with @testing-library/react instead - it won't work with the native elements from React Native.

When using @testing-library/react-native you can assert the presence of an element like so.

it("has the correct title in the button", () => {
    const { getByText } = render(<AppButton title="Hello" />);
    expect(getByText("HELLO")).toBeTruthy();
});
like image 75
juliomalves Avatar answered Dec 15 '22 18:12

juliomalves


You can use waitFor

Sample:

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


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

// or

waitFor(() => expect(getByTestId("Your-Test-Id")).toBeInTheDocument());
like image 45
sia Avatar answered Dec 15 '22 18:12

sia