October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Test a React Component with Jest

Render a component, interact with it, and assert what users see. Learn when to use React Testing Library, Jest snapshots, and fake timers.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To test a React component with Jest, render it, exercise it as a user would, and assert the visible result. For a web component, React Testing Library provides a practical DOM-testing pattern; Jest snapshots are another option for reviewing changes to rendered output. The examples below follow Jest’s version 30.0 React tutorial, so use your project’s existing configuration rather than treating its standalone setup as mandatory.

Choose what the test should verify

Most component tests should answer a concrete question about what someone sees or can do: does a label appear, does clicking a control change the output, or does the component respond correctly to its props? Jest’s React guide demonstrates two complementary approaches:

Approach What it checks Best fit Trade-off
DOM test with React Testing Library Rendered DOM and interactions, such as clicking a labeled checkbox Verifying user-visible behavior Write assertions around meaningful output and interaction rather than implementation details.
Snapshot with Jest and a test renderer Serialized rendered output against a saved reference Reviewing broad changes to a rendered tree A changed snapshot requires inspection to determine whether the change is intentional.

These methods can be used together. Jest’s documentation does not establish one as the right choice for every project.

Set up Jest in the context of your project

Start with the test runner and transform setup your application already uses. Jest’s version 30.0 React tutorial presents a standalone Babel-based example using babel-jest, @babel/preset-env, and @babel/preset-react; it lists React and React DOM as application dependencies. Its examples add @testing-library/react for DOM tests or react-test-renderer for snapshot tests. These are tutorial-specific examples, not a requirement to install both packages or replace an existing setup. See the Jest React tutorial for its configuration and package details.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The tutorial also shows a package script that invokes Jest with test. If your project already has a test script, use that project command instead of adding a duplicate one. The documentation uses current-version dependency placeholders; it does not prescribe package versions to copy into every project.

Write a DOM test for user-visible behavior

The essential pattern is to render the component, locate an element through the way a user can identify it, interact with it, and check the resulting output. Jest’s React tutorial demonstrates this with a checkbox: it finds the control by its label, clicks it, and checks that the displayed label changes.

  1. Render: render the component with the props or initial state for the scenario.
  2. Find: query for the control using a user-facing label or role.
  3. Interact: perform the relevant action, such as clicking the checkbox.
  4. Assert: check the meaningful output after the action, such as the changed label.

This structure keeps the test focused on the component’s observable behavior rather than how its internals happen to be implemented. The tutorial’s runnable component and test are in the official Jest React example.

Use snapshots as reviewable references

A snapshot test compares serialized rendered output with a reference stored by Jest. The reference is committed with the test, and Jest reports when later output differs. As the documentation puts it, “The next time you run the tests, the rendered output will be compared to the previously created snapshot.” — Jest, Testing React Apps, version 30.0.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A difference is a prompt to review, not an automatic indication that the component is broken or that the reference should be replaced. Inspect the diff and decide whether the output change is intended. Update the snapshot only when the new output is expected; otherwise, investigate the component or test change that caused it. A snapshot can help flag broad output changes, but it does not by itself explain whether a user interaction works as intended.

Test delayed behavior without waiting on real time

If a component schedules work with timers, real delays can make tests wait for elapsed time. Jest’s version 30.5 Timer Mocks guide explains how fake timers replace native timer functions so a test can control the passage of time. This lets a test advance scheduled work deliberately rather than waiting through the real delay. Follow the timer setup and cleanup pattern appropriate to your test in the Jest Timer Mocks guide.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep React Native testing separate

React Native components do not use the web DOM-testing example unchanged. Jest provides a separate React Native testing guide with platform-specific setup and testing tools. Use that guide for a native project rather than applying web DOM instructions wholesale.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.