4

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();
    });
  });
MrGrashopper
  • 41
  • 1
  • 1
  • 4
  • is your component rendering in test environment? – miraj May 22 '21 at 20:15
  • 1
    You should move your rendering of the component inside a beforeEach . – Shyam May 22 '21 at 20:25
  • yes it renders correctly. i see it on screen.debug() – MrGrashopper May 23 '21 at 01:32
  • when i put tree function inside a beforeEach then i get: ReferenceError: tree is not defined – MrGrashopper May 23 '21 at 01: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. – ggorlen May 23 '21 at 02:12

1 Answers1

4

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')
    });
  });
miraj
  • 546
  • 3
  • 12