Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Unable to find an element with the text when I try a test with Jest in React

I'm news with tests, se, here is my problem

I have that simple login component:

import React, { useState } from "react";

export default function Login() {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");

  function handleLogin(event) {
    event.preventDefault();
    console.log(email, password);
  }

  return (
    <form data-testid="login-form" onSubmit={handleLogin}>
      <input
        data-testid="useremail"
        type="email"
        placeholder="Email"
        value={email}
        onChange={event => setEmail(event.target.value)}
      />

      <input
        data-testid="userpassword"
        type="password"
        placeholder="Password"
        value={password}
        onChange={event => setPassword(event.target.value)}
      />
      <button onClick={handleLogin}>login</button>
    </form>
  );
}

I here my test attempty:

import React from "react";
import Login from "../pages/Login";

import { render, fireEvent } from "@testing-library/react";

describe("Login component", () => {
  it("user sent email and password", () => {
    const username = "[email protected]";
    const password = "123456";

    let { getByText, getByTestId } = render(<Login />);

    fireEvent.change(getByTestId("useremail"), {
      target: { value: username }
    });

    fireEvent.change(getByTestId("userpassword"), {
      target: { value: password }
    });

    fireEvent.submit(getByTestId("login-form"));

    expect(getByTestId("login-form")).toContainElement(
      getByText(username, password)
    );
  });
});

the error that returns: Unable to find an element with the text: [email protected]. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.

like image 954
Henrique Avatar asked Dec 17 '19 23:12

Henrique


People also ask

Can you test React with Jest?

Jest is a JavaScript testing framework that allows developers to run tests on JavaScript and TypeScript code and can be easily integrated with React JS. Open the package. json, and you will find that when you use create-react-app for creating a react project, it has default support for jest and react testing library.

Can Jest be used for functional testing?

- [Instructor] The Jest framework can be used to functionally test an application. The results can be included in a code coverage report, which is really useful. As a reminder, functional testing literally tests the functionality of an application.

Do you need Jest for React testing library?

Some familiarity with Jest as a test runner or framework is helpful but not required. Because Jest is pre-packaged with Create React App, you do not need to install it separately.

Is React testing library same as Jest?

React Testing Library is not an alternative to Jest. Each performs a clear task, and you need them both to test your components. 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.


1 Answers

getByText looks by text content. value attribute is not text content but just one of attributes. So it will not find input by this approach.

Also it's kind of not idiomatic to search by thing you are going to assert for. Better find element by it's static properties and then check for dynamic value:

expect(getByTestId("useremail").value).toEqual("[email protected]");

PS To be more "RTL-approach" I'd also suggest to use getByPlaceholderText instead of getByTestId.

like image 161
skyboyer Avatar answered Sep 28 '22 01:09

skyboyer