Skip to article content
FrontendXLesson 05 / 16

Unit 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 35 minutes · 4 exercises

  • Explain
  • Implement
  • Debug
  • Test

What you will be able to do

#
  • Run a test suite once in CI instead of watch mode.
  • Apply a coverage threshold as a ratchet.
  • Explain why deterministic tests are required for a reliable gate.

Overview

#

Unit tests as a gate

A unit test checks one behavior in isolation and runs in milliseconds. In CI it is the stage that catches logic regressions before slower component and end-to-end stages. The gate is meaningful only when the tests are deterministic: same input, same result, every run.

snippet.yaml
jobs:
  unit:
    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 -- --coverage

Run once, not in watch mode

A test runner in watch mode waits for file changes and never exits. On a runner that means the job hangs until it is killed by a timeout. CI must run the suite once and exit: vitest run, not vitest. This is one of the most common causes of a pipeline that appears stuck.

Coverage thresholds

A coverage threshold fails the gate when the measured coverage drops below a number. Treat it as a ratchet, not a target to game. High coverage with weak assertions proves little; a threshold that only ever rises keeps new untested code visible.

Determinism

Unit tests must not depend on the network, the wall clock, random values or ordering between files. Inject a clock, seed a random generator and reset shared state between tests. A test that passes only on a warm cache or a particular time zone is flaky and will fail the gate eventually.

Sandbox boundary

The playground can run a small pure-function test in the browser and report pass or fail. It cannot run the project's full Vitest suite with coverage across a repository. A browser assertion is a real assertion, but it is not the same as the CI unit 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 1Implement

    Add a unit job that runs the test suite once with coverage and fails if coverage is below the configured threshold.

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

    Reveal the reference answer
    reference.yaml
    jobs:
      unit:
        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 -- --coverage

    Authored expected output: A unit job that runs the suite once with coverage enabled.

    The coverage flag makes the runner apply the configured threshold and exit non-zero when the suite or the threshold fails.

  2. Exercise 2Debug

    This CI step never finishes because the test runner stays open. Fix the command.

    starter.yaml
          - run: pnpm vitest
    

    Reveal the reference answer
    reference.yaml
          - run: pnpm vitest run

    Authored expected output: The step uses vitest run so the suite executes once and exits.

    Plain vitest starts watch mode and waits for file changes, which hangs a CI job until the timeout kills it.

  3. Exercise 3Test

    Write a function assertEqual(actual, expected) that throws when the values differ and returns true when they match, using Object.is for comparison.

    starter.javascript
    function assertEqual(actual, expected) {
      // throw on mismatch, return true on match
    }

    Reveal the reference answer
    reference.javascript
    function assertEqual(actual, expected) {
      if (!Object.is(actual, expected)) {
        throw new Error('expected ' + String(expected) + ' but got ' + String(actual));
      }
      return true;
    }

    Authored expected output: assertEqual(2 + 3, 5) returns true; assertEqual(1, 2) throws.

    This is the core of any assertion library. It runs in the browser sandbox because it is pure logic with no runner dependency.

  4. Exercise 4Explain

    Explain why unit tests in CI must avoid the real network and the real clock.

    Reveal the reference answer
    reference.text
    The network and the clock are outside the test's control. A real network call can time out, return a different payload or be rate limited, and the real clock changes between runs. Either makes the result non-deterministic, so the same commit can pass and then fail. Injecting a fake transport and a fixed clock keeps the test reproducible.

    Authored expected output: A correct answer connects non-determinism to flaky, unreproducible failures.

    A gate must be reproducible. External state makes the same commit produce different results, which destroys trust in the gate.

Common misconceptions

#
  • Watch mode is fine in CI because the runner stops it.
  • Coverage percentage alone proves the tests are good.
  • Tests may use the real clock and network.

Follow-up practice

#
  • cicd-06-component-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.