React testing - What to test, getBy vs queryBy, fireEvent vs userEvent

2024. 6. 20. 16:35· React
목차
  1. What to test
  2. getByRole VS getByText
  3. getBy... VS queryBy...
  4. fireEvent VS userEvent 
This post is what I took notes from the React Testing for Beginners By Programming with Mosh.

What to test

Testing Components

  • How they render
  • How they responsd to user actions 

No tests > Bad tests

Bad tests are worse than No tests.

Poorly written tests can get in the way and slow us down. 

 

Test the behaviour (What the application does), not the implementation (How it's implemented)

 

 

Integration Tests might be slower than unit test, because of writing several unit tests together. 

But it gives us better confidence that the application works. 

Tests are less likely to break, if we refactor as long as the end result stays the same! 

 

getByRole VS getByText

 getByRole

semantic tags <button>, <heading> 

 

getByText
tags not having roles <div>, <p> 

 

getBy... VS queryBy...

getBy... throws an error if there's no element in the DOM.

it('should not render edit button if user is NOT admin', () => {
const user = {
id: 1,
name: 'hana',
isAdmin: false,
};
render(<UserAccount user={user} />);
const button = screen.queryByRole('button', { name: /edit/i });
expect(button).not.toBeInTheDocument();
});

 

queryBy... returns the matching node, and return null if no elements match.

 

queryByRole is more appropriate if I want to see test an element not to be in the document.

 

fireEvent VS userEvent 

the majority of the time you should probably use @testing-library/user-event.

fireEvent

  • doesn't simulate the real world scenario
  • a lightweight wrapper around the browser's low-level dispatchEvent API

userEvent 

  • manipulates the DOM just like a user interaction in the browser would

 

fireEvent dispatches DOM events, whereas user-event simulates full interactions, which may fire multiple events and do additional checks along the way.

 

 

References

https://testing-library.com/docs/user-event/intro

 

'React' 카테고리의 다른 글

React Testing Library test React Router - BrowserRouter vs MemoryRouter  (0) 2024.07.30
vite stage 개발/배포 모드 cli 설정  (0) 2024.07.17
Mui textfield, select 라벨 여부에 따른 조건부 스타일링 적용  (0) 2024.06.18
Turborepo turbo.json build outputs 빌드결과물 경로  (0) 2024.05.28
react daum postcode service api 우편번호 찾기+위경도 데이터 받아오기 컴포넌트로 분리  (0) 2024.04.26
  1. What to test
  2. getByRole VS getByText
  3. getBy... VS queryBy...
  4. fireEvent VS userEvent 
'React' 카테고리의 다른 글
  • React Testing Library test React Router - BrowserRouter vs MemoryRouter
  • vite stage 개발/배포 모드 cli 설정
  • Mui textfield, select 라벨 여부에 따른 조건부 스타일링 적용
  • Turborepo turbo.json build outputs 빌드결과물 경로
hana1203
hana1203
hana1203
hana's lab 🌱
hana1203
  • 분류 전체보기 (95)
    • React (38)
    • JavaScript (7)
    • Firebase (4)
    • Google Cloud Platform (6)
    • Flutter (3)
    • Python (7)
    • Computer Science (6)
    • Problem Solving (3)
    • Git (5)
    • 회고 (10)
    • Etc (3)

블로그 메뉴

  • 홈

공지사항

전체
오늘
어제

인기 글

태그

  • 티스토리챌린지
  • 오블완

최근 글

최근 댓글

hELLO · Designed By 정상우.v4.2.2
hana1203
React testing - What to test, getBy vs queryBy, fireEvent vs userEvent
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.