Prompt
How do I run end-to-end tests on a React app without a huge maintenance burden?
Latest observation
To keep end-to-end tests for a React app useful without becoming a maintenance nightmare, the big idea is:
Test user flows, not implementation details.
Keep the number of E2E tests small, stable, and focused on critical paths.
Here’s a practical approach.
1) Use E2E tests only for high-value flows
Don’t try to cover everything with E2E tests. They’re slower and more brittle than unit/integration tests.
Good E2E candidates:
- Sign up / log in
- Checkout / payment
- Create/edit/delete an important record
- Critical navigation paths
- Permissions / role-based access
- Smoke tests for main pages
Avoid using E2E for:
- Every edge case
- UI component behavior
- Detailed validation logic
- Exhaustive form permutations
Those are better handled by:
- Unit tests
- Component tests
- Integration tests
2) Prefer Cypress or Playwright
For most React apps today, Playwright or Cypress are the common choices.
Playwright
- Great cross-browser support
- Strong auto-waiting
- Good parallelization
- Very solid for modern apps
Cypress
- Very popular and easy to get started with
- Great developer experience
- Strong UI debugging
- Very good if your team already uses it
If you’re starting fresh, I’d usually lean Playwright unless your team already knows Cypress well.
3) Select elements using stable selectors
A huge source of test maintenance is brittle selectors like:
.sidebar > div:nth-child(3) > button- text that changes often
- CSS classes generated by libraries
Instead, add stable attributes like:
<button data-testid="save-profile">Save</button>
Then test with:
data-testid- accessible roles/labels where possible
Example:
await page.getByRole('button', { name: 'Save' }).click();
Best practice:
- Prefer accessible selectors first (
getByRole,getByLabel) - Use
data-testidonly when necessary
This makes tests more robust and improves accessibility too.
4) Keep tests independent
Each test should be able to run on its own.
Avoid:
- One test depending on another test’s output
- Shared mutable state between tests
- Manual “run these in order” assumptions
Instead:
- Set up required state in
beforeEach - Use test fixtures/factories
- Reset the database or use isolated test data
Example idea:
- Create a user via API before the test
- Log in directly through the backend/session setup
- Test the UI flow from there
This reduces test flakiness and makes failures easier to diagnose.
5) Use API setup instead of UI setup when possible
If a test’s goal is to verify the UI flow after a user is authenticated, don’t waste time clicking through the login form every time.
Better:
- Create test data through APIs
- Seed the database
- Authenticate via programmatic login or session storage
This keeps tests:
- Faster
- Less flaky
- Easier to maintain
Example:
- Use API calls to create a project
- Open the app already signed in
- Test that the project appears and can be edited
6) Mock only external dependencies, not your own app logic
A common maintenance trap is over-mocking.
Good to mock:
- Third-party payment providers
- Email/SMS services
- Analytics
- Unreliable external APIs
Usually avoid mocking:
- Your app’s own backend responses too aggressively
- Core business logic that you actually want to validate end-to-end
You want your E2E tests to prove the whole system works together. If you mock too much, they stop being true end-to-end tests.
7) Make test data disposable
Use test-specific data that can be created and destroyed freely.
Good patterns:
- Prefix test records with
e2e- - Use unique IDs/timestamps
- Clean up after tests if needed
- Run tests against a dedicated environment
This prevents:
- Collisions between tests
- Pollution of shared dev/staging data
- Hidden dependencies on old records
8) Minimize visual assertions
Unless your app is highly visual, don’t rely on screenshot comparisons for everything.
Use visual tests sparingly for:
- Layout regressions
- Design-critical pages
- Dark mode / responsive breakpoints
But for most functionality:
- Assert behavior and state
- Check that the right page loads
- Verify key text, roles, and data
Too many pixel-based tests can create noisy failures.
9) Make failures easy to debug
A low-maintenance E2E suite is one where failures are easy to understand.
Helpful practices:
- Capture screenshots/videos on failure
- Log network errors
- Name tests clearly
- Keep test steps short and readable
Example test names:
- “User can update profile name”
- “Admin can deactivate a user”
- “Checkout succeeds with valid card”
If a test fails, you should know what user outcome broke.
10) Run them in CI, but not on every tiny change if they’re expensive
A good setup is:
- Run unit/component tests on every commit
- Run E2E tests on merge/pull request
- Run a smaller smoke subset on each push if needed
- Run the full suite nightly or before release
This balances confidence and speed.
You can also:
- Split fast smoke tests from slower full regression tests
- Run only relevant E2E subsets when possible
11) Treat flaky tests as a bug
Flaky tests destroy trust quickly.
When a test is flaky:
- Fix it immediately or quarantine it
- Don’t just re-run until it passes
- Identify whether the issue is timing, data, environment, or selector instability
Common causes:
- Poor waits
- Animations/transitions
- Network timing
- Shared state
- Race conditions
Use your test runner’s auto-waiting and avoid arbitrary sleep() calls.
12) Structure tests around user journeys
A simple pattern that works well:
Example test structure
- Arrange: create data / authenticate
- Act: perform the user action
- Assert: verify the outcome
Example:
test('user can update profile', async ({ page }) => {
await loginAsTestUser(page);
await page.goto('/profile');
await page.getByLabel('Display name').fill('New Name');
await page.getByRole('button', { name: 'Save' }).click();
await expect(page.getByText('Profile updated')).toBeVisible();
});
That’s easy to read and easy to maintain.
A good maintenance strategy in one sentence
Use a small number of high-value E2E tests, stable selectors, isolated setup, and rely on lower-level tests for the rest.
Recommended testing pyramid
A practical mix for React apps:
- Lots of unit tests: pure logic, utilities, reducers
- Some integration/component tests: forms, UI state, API interaction
- Few E2E tests: critical business flows
That pyramid keeps E2E from becoming your main test burden.
If you want the shortest possible checklist
- Use Playwright or Cypress
- Test only critical flows
- Use
getByRole/getByLabel/data-testid - Create test data via API, not the UI
- Keep tests independent
- Mock only external services
- Run in CI and fix flakes fast
If you want, I can also give you:
- a Playwright setup template for a React app, or
- a recommended folder structure and test strategy for a real project.
Brands