PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

React · Theory

Unit Testing in React

← All stacks

Theory

61/94

Unit Testing in React

React Testing Library: render(<Comp />), screen.getByRole / getByText, userEvent.click, assert the text/role the user sees. Jest or Vitest runs it. Test behaviour, not implementation (not wrapper.state(), not CSS class internals).

Example: Counter — click Increment, expect ‘1’ on screen. That’s a unit-style component test. MSW mocks network. Don’t snapshot the entire HTML of a page as your only test.

Unit Testing in React — output — render / click / assert text. Don’t test implementation details like state keys.

Exam tip

RTL render + userEvent + assert text.

Example

// RTL idea
console.log("render(<Counter />)");
console.log("click / increment");
console.log('expect(screen.getByText("1")).toBeInTheDocument()');

Unit Testing in React — output: render / click / assert text. Don’t test implementation details like state keys.

Short notes

  • Lib : RTL + Vitest/Jest.
  • Ruleuser behaviour, not internals.
  • Exclick → text on screen.

Questions

1

Explain Unit Testing as if you are teaching a junior — definition, then one tiny UI.

2

What does the example show on screen (or print), and what does that prove?

3

What mistake do freshers make with Unit Testing?

Previous← Virtual DOM in ReactNextCarousel in React →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.