Testing strategies patterns

From farming-labs/fm-skills · View interactive version

Testing strategies patterns is an AI agent skill from the GitHub repository farming-labs/fm-skills. Explains testing strategy for modern web applications including unit, integration, component, end-to-end, contract, accessibility, visual, and performance testing. Use when deciding what to test at each layer, choosing modern testing tools, designing CI quality gates, or improving confidence without

How to use this skill

Skill file

Testing Strategies Patterns

Overview

Testing strategy is about deciding where confidence should come from. Strong teams do not try to prove everything with one kind of test. They distribute risk across fast local checks, realistic integration tests, and a smaller number of end-to-end flows.

Modern tooling has improved this significantly. Tools like Vitest, Playwright, Testing Library, Cypress, Storybook, MSW, and contract-testing frameworks make it practical to build layered test suites that are both fast and credible.

The Goal

The goal is not "maximum coverage" in the abstract. The goal is:

Test Layers

| Layer | What It Verifies | Speed | Common Tools |

|------|-------------------|-------|--------------|

| Static analysis | Types, lint, obvious mistakes | Fastest | TypeScript, ESLint, Biome |

| Unit | Pure logic in isolation | Very fast | Vitest, Jest |

| Component | UI behavior with realistic rendering | Fast | Testing Library, Vitest, Cypress Component |

| Integration | Multiple modules or network boundaries together | Medium | Vitest, Testing Library, MSW, Supertest |

| End-to-end | Real user flows in a running app | Slowest | Playwright, Cypress |

| Contract | API compatibility between systems | Medium | Pact, schema validation |

| Accessibility | Keyboard, semantics, announcements | Fast to medium | axe, Playwright, Testing Library |

| Visual | Layout and rendering regressions | Medium | Storybook, Chromatic, Playwright screenshots |

| Performance | Real-user or lab performance budgets | Medium | Lighthouse CI, Web Vitals, Playwright traces |

Do not treat these as competing options. They are complementary.

Testing Pyramid, But Updated

The classic pyramid still matters:

          Small number of E2E tests
        More integration/component tests
     Large number of unit and static checks

But modern frontend teams often use a modified shape:

This is more useful than a simplistic "only unit tests" or "only E2E" approach.

What to Test Where

Unit Tests

Use unit tests for:

import { describe, it, expect } from 'vitest';
import { calculateTotal } from './cart';

describe('calculateTotal', () => {
  it('applies discount before tax', () => {
    expect(calculateTotal({ subtotal: 100, discount: 10, taxRate: 0.1 })).toBe(99);
  });
});

Avoid unit-testing framework internals or trivial wrappers with no meaningful behavior.

Component Tests

Use component tests for:

render(<LoginForm />);
await user.type(screen.getByLabelText(/email/i), 'user@example.com');
await user.click(screen.getByRole('button', { name: /sign in/i }));
expect(await screen.findByText(/welcome/i)).toBeInTheDocument();

Prefer assertions that reflect user-observable behavior rather than internal implementation details.

Integration Tests

Use integration tests when behavior spans several parts:

Good integration tests catch the gaps between units, which is where many bugs actually live.

End-to-End Tests

Use E2E for:

E2E tests should prove that the system works as assembled. They should not be your only safety net.

Modern Tooling Map

Vitest

Good fit for:

Why teams like it:

Jest

Still valid when:

Choose based on repo fit, not trend-chasing.

Testing Library

Use it to test UI the way users experience it:

Playwright

Strong choice for modern E2E and browser automation:

For many modern teams, Playwright is the default E2E recommendation.

Cypress

Still useful for:

Choose it if it fits the stack and team habits better than Playwright.

MSW (Mock Service Worker)

Use for:

This often produces better tests than mocking every API call manually.

Storybook

Use for:

Storybook is not a replacement for tests, but it amplifies component confidence when paired with automated checks.

Mocking Strategy

Mock carefully. Over-mocking creates fake confidence.

Good uses of mocks:

Bad uses of mocks:

Prefer:

Component vs E2E Boundaries

If a behavior can be proven reliably in a component or integration test, do it there first.

Reserve E2E for:

This keeps E2E suites small, meaningful, and maintainable.

Contract Testing

Contract tests reduce breakage between frontend and backend teams.

Use them when:

Examples:

Accessibility Testing

Accessibility should be part of the testing strategy, not a separate audit phase.

Use automated checks for:

Also include manual checks for:

Automated tooling catches some issues, not all of them.

Visual Regression Testing

Use visual tests selectively:

Keep them stable by:

Visual diffs become noisy if applied indiscriminately.

Performance and Reliability Checks

Modern test strategy often includes non-functional gates:

These checks elevate a repo because they guard user experience, not just correctness.

CI Strategy

Split CI into layers:

Fast checks on every change

Heavier checks on merge or release

This keeps feedback fast while still protecting critical paths.

Flaky Test Prevention

Flakes usually come from timing, shared state, or unrealistic assertions.

Reduce flakes by:

If a suite is flaky, do not normalize reruns as the main solution. Fix the source of nondeterminism.

Coverage Guidance

Coverage is a signal, not proof.

Useful:

Misleading:

Prefer risk-based coverage goals over vanity percentages.

Decision Matrix

| Problem | Best Starting Layer |

|--------|----------------------|

| Pure calculation or utility logic | Unit |

| Form interaction or loading state | Component |

| Route loader, API call, cache behavior | Integration |

| Sign-in, checkout, core business journey | End-to-end |

| Backend/frontend schema drift | Contract |

| Focus, keyboard flow, labels | Accessibility |

| Layout or styling drift | Visual |

| Performance regressions | Performance budget tooling |

Practical Starter Stack

For a modern web app, a strong default stack is:

This is not mandatory, but it is a credible modern baseline that will elevate most repos without overcomplicating them.

Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.