Testing ensures that refactoring, upgrading dependencies, and adding features do not break existing functionality.
In the past, Create React App paired with Jest was standard. In modern Vite-powered engineering:
Vitest: A blazing-fast, Vite-native unit test runner. It uses the exact same plugins and configuration as Vite, running tests 5 to 10 times faster than Jest with full TypeScript and ESM support.
React Testing Library (RTL): A testing utility based on a simple, profound philosophy:
> "The more your tests resemble the way your software is used, the more confidence they can give you."
1import'@testing-library/jest-dom';// Adds custom DOM matchers like toBeInTheDocument()
3. The RTL Query Priority
React Testing Library intentionally guides you away from querying by CSS classes or internal implementation IDs. Instead, it encourages queries that mimic how real users (and assistive technologies) find elements:
Priority
Query Method
Rationale
1. Accessible to Everyone
getByRole('button', { name: /save/i })
Queries semantic ARIA roles and labels
2. Semantic Form Queries
getByLabelText('Email')
Simulates user locating input by its label
3. Placeholder / Text
getByPlaceholderText('Search...')
Queries visible placeholder or screen text
4. Display Text
getByText('Welcome back')
Finds non-interactive text content
5. Test ID (Last Resort)
getByTestId('custom-canvas')
Reserved for non-semantic canvas or svg elements
4. Difference Between getBy, queryBy, and findBy
Prefix
Behavior When NOT Found
Primary Use Case
getBy...
Throws an immediate error
Expecting element to exist synchronously
queryBy...
Returns null (does NOT throw)
Asserting element does NOT exist (expect(queryByText(...)).not.toBeInTheDocument())
findBy...
Returns a Promise (waits up to 1s)
Waiting for asynchronous data or animations
Interactive Quiz• Knowledge Check
Practice Quiz
Question 1
What is the core philosophy of React Testing Library?
Question 2
Why is Vitest preferred over Jest in modern Vite projects?
Question 3
Which query should you use to assert that an element is NOT present in the DOM?
Question 4
Which query method must be used when an element will appear asynchronously after an API call?
Question 5
What is the recommended query method for locating a submit button according to RTL priority guidelines?
Next Lesson
Writing Unit Tests for Functional Components
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.