Skip to article content
FrontendXLesson 23 / 24

Wiring Tests into CI Quality Gates

Build a working model of automated testing from assertions to unit, component, API, integration, contract and end-to-end tests, then apply it to a single progressive task-board capstone where each milestone adds a real defect, a failing test, a corrected implementation and an expected result.

intermediate · about 35 minutes · 6 exercises

  • Predict
  • Explain
  • Implement
  • Debug
  • Test
  • Interview

What you will be able to do

#
  • Describe the stages of a test pipeline and what each blocks.
  • Choose which suites run on every change versus on a schedule.
  • State which environments can run each stage.

Overview

#

A gate blocks a merge or a deploy

A quality gate is a check that must pass before code moves forward. Tests become gates when the pipeline runs them and refuses to continue on failure. A test that nobody runs on change is documentation, not a gate.

Stage the pipeline by cost

The fast checks run first so failures surface in seconds: lint, type check, unit tests. Then integration tests. Then the slow, expensive stages: end-to-end browser tests, visual regression and load tests. Ordering by cost gives fast feedback and avoids spending ten minutes on a browser suite when a unit test would have failed immediately.

What each stage blocks

A unit test failure blocks the change. An end-to-end failure on the critical journey blocks the deploy. A visual diff may block or may warn, depending on how much review the team wants. A load test usually runs before a release rather than on every commit. Decide the blocking behavior deliberately, because a gate that is always ignored is worse than no gate.

Environment capability

CI runners are full machines, so they can run native browser automation, spawn Node processes and run the whole suite. An in-browser sandbox cannot. The pipeline is where the native and browser tests that cannot run in a sandbox actually run. This is why the capstone labels each milestone with the environment that can execute it.

Artifacts for diagnosis

When a stage fails, the pipeline should publish artifacts that make diagnosis possible: test reports, screenshots from failed browser tests, coverage reports and logs. A failure with no artifact forces a local reproduction, which is slow. Always upload the failing evidence.

Caching and determinism

Caching dependencies speeds up the pipeline but can hide a missing lockfile entry. Pin dependency versions and cache keys to the lockfile. A pipeline that passes because of a stale cache is not a reliable gate.

Required versus advisory checks

Make correctness checks required: lint, types, unit, integration and the critical end-to-end journey. Make noisy checks advisory until they are stable: visual diffs and performance budgets. Promote an advisory check to required only after it has proven stable across many runs. A required flaky check blocks everyone and erodes trust.

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 unit test fails on the first stage. What happens to the later stages?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.javascript
    // stage 1: unit tests -> FAIL
    // stage 2: integration
    // stage 3: e2e
    Choose the best answer
  2. Exercise 2Explain

    Explain why a required flaky check is worse than no check.

    Reveal the reference answer
    reference.javascript
    A flaky check fails for reasons unrelated to the change, so it blocks correct work and forces reruns. People learn to ignore the red result or to retry until green, which means a real failure can slip through unnoticed. A check that is sometimes wrong trains the team to distrust all checks. It is better to fix the flakiness or keep the check advisory until it is stable than to require an unreliable gate.

    The answer should connect flakiness to lost trust and to real failures being ignored.

  3. Exercise 3Implement

    Implement `runStages(stages)` that runs each stage in order and stops at the first failure, returning the index of the failed stage or -1 if all pass. A stage is `{ name, run }` where `run()` returns true on success. `console.log(runStages([{name:'a',run:()=>true},{name:'b',run:()=>false},{name:'c',run:()=>true}]))` should print `1`.

    starter.javascript
    function runStages(stages) {
      // your code here
    }

    Reveal the reference answer
    reference.javascript
    function runStages(stages) {
      for (let i = 0; i < stages.length; i++) {
        if (!stages[i].run()) return i;
      }
      return -1;
    }

    Authored expected output: 1

    The loop runs stages in order and returns the index of the first failure. Stage b fails at index 1, so the function returns 1 and never runs stage c, which models a fail-fast pipeline.

  4. Exercise 4Debug

    This pipeline runs the slow e2e suite before the fast unit tests, so a broken unit test still pays for e2e first. Reorder the stages by cost so the fast checks run first, and print the stages that ran before the failure so `console.log(...)` prints `lint,unit`.

    starter.javascript
    const stages = [
      { name: 'e2e', run: () => true },
      { name: 'unit', run: () => false },
      { name: 'lint', run: () => true },
    ];
    const ran = [];
    for (const stage of stages) {
      ran.push(stage.name);
      if (!stage.run()) break;
    }
    console.log(ran.join(','));

    Reveal the reference answer
    reference.javascript
    const stages = [
      { name: 'lint', run: () => true },
      { name: 'unit', run: () => false },
      { name: 'e2e', run: () => true },
    ];
    const ran = [];
    for (const stage of stages) {
      ran.push(stage.name);
      if (!stage.run()) break;
    }
    console.log(ran.join(','));

    Authored expected output: lint,unit

    In the starter the slow e2e stage runs first, so the log shows 'e2e,unit' and the wasted minutes are visible. Ordering by cost puts lint and unit before e2e, so the run stops at the failing unit stage after only the fast checks, and the log shows 'lint,unit'.

  5. Exercise 5Test

    Write a test that proves `runStages` stops at the first failure and does not run later stages.

    starter.javascript
    function runStages(stages) {
      for (let i = 0; i < stages.length; i++) {
        if (!stages[i].run()) return i;
      }
      return -1;
    }

    Reveal the reference answer
    reference.javascript
    function assertEqual(actual, expected, label) {
      if (actual !== expected) throw new Error(label + ': expected ' + expected + ' but got ' + actual);
    }
    const ran = [];
    const result = runStages([
      { name: 'a', run: () => { ran.push('a'); return true; } },
      { name: 'b', run: () => { ran.push('b'); return false; } },
      { name: 'c', run: () => { ran.push('c'); return true; } },
    ]);
    assertEqual(result, 1, 'failed index');
    assertEqual(ran.join(','), 'a,b', 'stopped before c');
    assertEqual(runStages([{ name: 'x', run: () => true }]), -1, 'all pass');
    console.log('pipeline verified');

    Authored expected output: pipeline verified

    The recorded run list proves stage c never executed, which is the fail-fast behavior. The final assertion covers the all-pass case returning -1.

  6. Exercise 6Interview

    In an interview, explain how you would design a CI pipeline for a frontend application.

    Reveal the reference answer
    reference.javascript
    I order stages by cost: lint and type check, then unit tests, then integration, then end-to-end browser tests, then visual and performance checks. The fast stages are required and block the merge; the noisy stages start advisory and are promoted once stable. I publish artifacts on failure such as reports and screenshots so diagnosis does not need a local reproduction. I pin dependency versions and cache keys to the lockfile. The native and browser stages run on the runner, not in a sandbox.

    The answer should show cost ordering, required versus advisory gates, failure artifacts, and accurate environment claims.

Common misconceptions

#
  • Every check should run on every commit regardless of cost.
  • A failing check that is ignored is still a useful gate.
  • A browser sandbox can run the same CI pipeline as a runner.

Follow-up practice

#
  • testing-test-strategy
  • testing-capstone-quality-task-board

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.