5

i can't get the test id within my jest test. I am new to react testing. I want to test this component and get back for example the h3 text what was given before.

Tells me:
"TestingLibraryElementError: Unable to find an element by: [data-testid="testH3"]"

Can you help me?

// Component
import React from 'react';
import './Card.scss'; 
import Card from '@material-ui/core/Card';
import { Link } from "react-router-dom";

function MovieCard({title, image}) {
    return (
        <div className="card__container">
            <Link to={"/details/" + title}>
                <Card className="card">
                    <img src={image} alt=""/>
                    <div className="card__info">
                        <h3 
                            data-testid="testH3"
                            className="card__title">{ title }
                        </h3>
                    </div>
                </Card>            
            </Link>
        </div>
    )
}

export default MovieCard

// test
import React from 'react';
import { render, screen, cleanup } from '@testing-library/react';
import MovieCard from '../MovieCard';
import { BrowserRouter as Router } from "react-router-dom";

afterEach(cleanup);

describe('Test MovieCard', () => {
  const tree = render(
    <Router>
      <MovieCard title="Batman" image="imagesrc"/>
    </Router>
  )

  it('should match with snapshot', () => {
    screen.debug();
    expect(tree).toMatchSnapshot();
   });

   it('test h3', () => {
     const {getByTestId} = tree;
     const element = screen.getByTestId('testH3')
    });
  });

UPDATE:

Final code works now pretty well. Thank you guys

import React from 'react';
import { render, screen, cleanup } from '@testing-library/react';
import MovieCard from '../MovieCard';
import { BrowserRouter as Router } from "react-router-dom";

afterEach(cleanup);

describe('Test MovieCard', () => {
  function tree() {
    return render(
      <Router>
        <MovieCard title="Batman" image="imagesrc"/>
      </Router>
    )
  }

  it('should match with snapshot', () => {
    expect(tree()).toMatchSnapshot();
   });

   it('test h3 text is Batman', () => {
     const {getByTestId} = tree();
     const element = screen.getByTestId('testH3')
     expect(element.textContent).toEqual("Batman")
     screen.debug();
    });
  });
5
  • is your component rendering in test environment? Commented May 22, 2021 at 20:15
  • 1
    You should move your rendering of the component inside a beforeEach . Commented May 22, 2021 at 20:25
  • yes it renders correctly. i see it on screen.debug() Commented May 23, 2021 at 1:32
  • when i put tree function inside a beforeEach then i get: ReferenceError: tree is not defined Commented May 23, 2021 at 1:55
  • 1
    The beforeEach should set let variables that are in the describe callback scope. You could also move the rendering into each it block which achieves the same goal: keeping each it block state separate. Never let a rendered component carry from one it block to the next. This makes tests unreliable and hard to trust. Commented May 23, 2021 at 2:12

1 Answer 1

5

use function instead of const

describe('Test MovieCard', () => {
  function tree() {
    return render(
      <Router>
        <MovieCard title="Batman" image="imagesrc"/>
      </Router>
    )
  }

  it('should match with snapshot', () => {
    screen.debug();
    expect(tree()).toMatchSnapshot();
   });

   it('test h3', () => {
     const {getByTestId} = tree();
     const element = screen.getByTestId('testH3')
    });
  });
Sign up to request clarification or add additional context in comments.

7 Comments

when I do this i get another error: TypeError: Cannot destructure property 'getByTestId' of 'tree(...)' as it is undefined.
this is obvious as i didn't return that rendered component. Updated the answer. could you check again?
Yes it works now thank you. final code above in update
Interesting. Worked for me.
@MrGrashopper - If this worked for you, then, please mark accept this as the answer, so that others may benefit from it along with the author. Thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.