Skip to article content
FrontendXLesson 06 / 16

Component Test Gates

Take one task-board application from local tests to a real delivery pipeline: continuous integration fundamentals, push and pull-request quality gates, lint, type-check, unit, component and end-to-end gates, dependency caching, matrix builds, artifacts and checksums, secrets and supply-chain security, release promotion, staging and production, rollback and observability.

intermediate · about 30 minutes · 4 exercises

  • Predict
  • Implement
  • Debug
  • Interview

What you will be able to do

#
  • Choose between a simulated DOM and a real browser for component tests.
  • Query components the way a user and a screen reader do.
  • Identify sources of component-test flakiness.

Overview

#

Component tests need a DOM

A component test renders a component and queries the result. Rendering needs a DOM. In CI you have two options: a simulated DOM such as jsdom or happy-dom inside Node, or a real browser through a browser-mode runner. The simulated DOM is faster; the real browser is more faithful. Most pipelines run the simulated DOM on every pull request and reserve real-browser runs for a smaller, targeted set.

snippet.yaml
jobs:
  component:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: corepack enable && pnpm install --frozen-lockfile
      - run: pnpm test

Query like a user

Query by role and accessible name, not by test id or CSS class. A role query tests the same thing a screen reader uses, so a passing test is also evidence that the element is reachable. Test ids are invisible to users and can pass while the accessible name is broken.

Accessibility as a gate

A component test can assert that a dialog has a role, that a button has an accessible name and that focus moves where it should. These assertions turn accessibility from a manual audit into an automated gate. They do not replace a full audit, but they catch regressions cheaply.

Stability

Component tests become flaky when they depend on real timers, animation frames or network responses. Use fake timers, await the state change explicitly and stub the network. A component test that needs a retry is a component test that is not yet deterministic.

Sandbox boundary

The browser sandbox can render a small component and assert on the DOM, because it already is a browser. It cannot run the project's full component suite with its build tooling and coverage. The sandbox demonstrates the technique; the CI job enforces it across the repository. The quality-task-board project defines no separate component-test script, so this job calls the shared test script (Vitest); the component name only labels the gate.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    A component test queries the DOM. What does it need to render?

    Choose the best answer
  2. Exercise 2Implement

    Add a component-test job that installs dependencies and runs the component tests through the project's test script.

    starter.yaml
    jobs:
      component:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
    

    Reveal the reference answer
    reference.yaml
    jobs:
      component:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - uses: actions/setup-node@v4
            with:
              node-version: 20
          - run: corepack enable && pnpm install --frozen-lockfile
          - run: pnpm test

    Authored expected output: A component job that installs dependencies and runs the project's test script (pnpm test).

    Separating component tests from unit tests keeps the failure message specific and lets the component job use a DOM environment. In the quality-task-board project there is no separate component script, so the job calls `pnpm test` (Vitest) and the job name only labels the gate.

  3. Exercise 3Debug

    This test queries by a test id that was removed, so it fails even though the button works. Rewrite the query to use the accessible role and name.

    starter.javascript
    const button = screen.getByTestId('submit-btn');
    expect(button).toBeTruthy();
    

    Reveal the reference answer
    reference.javascript
    const button = screen.getByRole('button', { name: 'Submit' });
    expect(button).toBeTruthy();
    

    Authored expected output: The query uses getByRole with the accessible name Submit.

    A role and name query is resilient to markup changes and asserts that the element is reachable by assistive technology.

  4. Exercise 4Interview

    How do you keep component tests fast and stable in CI?

    Reveal the reference answer
    reference.text
    Run them in a simulated DOM for speed on every pull request, reserve real-browser runs for a smaller targeted set, query by role and accessible name, use fake timers, await explicit state changes and stub the network. Treat any test that needs a retry as non-deterministic and fix the cause.

    Authored expected output: A plan covering environment choice, query style, deterministic time and network, and no reliance on retries.

    Speed comes from the simulated DOM and targeted real-browser runs. Stability comes from deterministic time, network and queries.

Common misconceptions

#
  • Component tests require a real browser.
  • Test ids are the most reliable query.
  • A retry makes a flaky component test acceptable.

Follow-up practice

#
  • cicd-07-e2e-test-gates

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.