← Back to Tutorials Chapter 11

Testing & Deployment

Why Test React Apps?

Automated tests catch regressions, document expected behavior, and give you confidence when refactoring. The React ecosystem standard on Jest (test runner) and React Testing Library (RTL) for component tests.

Unit Testing with Jest & RTL

Jest provides describe, it/test, expect, and mock. RTL provides utilities to render components and query rendered output.

import { render, screen, fireEvent } from "@testing-library/react";
import Counter from "./Counter";

test("increments count when button is clicked", () => {
  render(<Counter />);
  const button = screen.getByRole("button", { name: /increment/i });
  fireEvent.click(button);
  expect(screen.getByText("1")).toBeInTheDocument();
});

Snapshot Testing

Snapshot tests capture the rendered output and compare it to a stored reference file. Use them for UI components that rarely change.

import { render } from "@testing-library/react";
import Header from "./Header";

test("Header matches snapshot", () => {
  const { container } = render(<Header title="Home" />);
  expect(container.firstChild).toMatchSnapshot();
});
Snapshots are stored in __snapshots__ folders alongside your test file. Run jest --updateSnapshot to refresh them after intentional changes.

Testing Hooks

Use renderHook from RTL to test custom hooks outside of a component.

import { renderHook, act } from "@testing-library/react";
import useCounter from "./useCounter";

test("useCounter increments value", () => {
  const { result } = renderHook(() => useCounter(0));
  act(() => result.current.increment());
  expect(result.current.count).toBe(1);
});

React CLI Commands

Create React App provides four commands via react-scripts:

React build and deployment pipeline

Building for Production

Running npm run build generates minified, tree-shaken bundles with content hashes for cache busting. The build/ folder can be served by any static file server.

npx create-react-app my-app
cd my-app
npm run build
# Output in build/ ready for deployment

Deploying to Vercel

Vercel is optimized for frontend apps. Connect your Git repository or use the CLI:

npm install -g vercel
vercel
# Follow prompts — it auto-detects Create React App

Deploying to Netlify

Netlify supports drag-and-drop of the build/ folder or Git-based CI/CD. Set the build command to npm run build and publish directory to build.

Deploying to GitHub Pages

For GitHub Pages, install gh-pages and add a homepage field to package.json.

npm install --save-dev gh-pages

// package.json
"homepage": "https://yourname.github.io/my-app",
"scripts": {
  "predeploy": "npm run build",
  "deploy": "gh-pages -d build"
}

npm run deploy

Example Project Walkthrough

A typical full-stack React project might include a Node/Express API, a PostgreSQL database, and a React frontend. Alternatively, a static site (e.g. a portfolio blog) is deployed directly from the build/ folder to any static host.

For SPAs with client-side routing, ensure your host redirects all paths to index.html (Vercel and Netlify do this automatically; for GitHub Pages you need a 404.html or a custom .htaccess).

Exercise: Test and Deploy a React App

  1. Write a unit test for a TodoList component using RTL — test render, add, and delete.
  2. Add a snapshot test for a Header component.
  3. Run npm run build and inspect the output in the build/ folder.
  4. Deploy the built app to Vercel or Netlify using either the CLI or Git-based workflow.
  5. Configure redirect rules so that client-side routes work on refresh.