Testing in a CI/CD Pipeline

A CI pipeline is a series of checks that run automatically on every push or PR (pull request), such as lint (a checker for common mistakes in code), tests…

A teammate merges their PR (pull request) with confidence: "I ran the tests, don't worry." It turns out they ran a single test file, and lint (a checker for common mistakes in code, such as an unused variable) never ran. The next day main is broken for everyone on the team.

A CI pipeline is a series of checks that run automatically on every push or pull request: lint, tests, then E2E. That way, lint and tests no longer depend on anyone's memory. Picture a company's hiring process with three stages in a row: a check of the application papers, a written task, then an interview. An applicant who fails the paper check isn't called to the written task, and no applicant is hired before passing all three stages. In this picture, the applicant is your code change, the three stages are lint, tests and E2E, and being hired means the change may be merged.

A GitHub Actions workflow

GitHub Actions (GitHub's CI service) reads YAML files in the .github/workflows/ folder. One file is one workflow. Here is one, with three jobs:

# .github/workflows/ci.yml
name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: 24
          cache: npm
      - run: npm ci
      - run: npm run lint

  test:
    needs: lint
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        node-version: [22, 24]
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: ${{ matrix.node-version }}
          cache: npm
      - run: npm ci
      - run: npx vitest run --coverage

  e2e:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: 24
          cache: npm
      - run: npm ci
      - run: npx playwright install --with-deps
      - run: npx playwright test

When the workflow runs

The on section sets the events (things that happen) that trigger the workflow:

Ordering jobs with needs

Without needs, every job runs at the same time. needs: lint means test waits for lint to pass first. needs: test means e2e waits for every test job: test (22) and test (24), made by the matrix (next section). This table shows each job's status in four cases:

What's broken lint test (22) test (24) e2e
nothing passes passes passes passes
an unused variable fails skipped skipped skipped
the shipping test fails passes fails fails skipped
RegExp.escape is used passes fails passes skipped

When lint fails, the jobs that wait for it don't run at all, and their status is skipped. e2e, which waits for test, doesn't run either. This is fail fast: cheap, quick checks go first, so the slow E2E run doesn't waste time on code that's clearly broken.

This is the lint job's log when a variable is never used:

Run npm run lint

> [email protected] lint
> eslint .


/home/runner/work/shop/shop/src/shipping.js
  3:9  error  'discount' is assigned a value but never used  no-unused-vars

✖ 1 problem (1 error, 0 warnings)

Error: Process completed with exit code 1.

Read this log from the bottom up. exit code 1 means the command failed. Above it is the reason, with the file, line 3 and column 9: the variable discount is given a value but never used.

Matrix: one job, several versions

strategy.matrix runs the same job for each combination of values, in parallel (at the same time). node-version: [22, 24] produces two jobs, test (22) and test (24). Two lists, say 3 operating systems and 2 Node versions, produce 3 × 2 = 6 jobs.

You see why the moment your code uses a new feature. RegExp.escape only exists from Node 24, so the job on Node 22 fails:

 FAIL  src/search.test.js > highlights the searched word
TypeError: RegExp.escape is not a function

On your laptop, running Node 24, everything is green. Without a matrix, the break on a server still on Node 22 would only show after deploying. fail-fast: false keeps every combination running to the end, so you see the result for each version. The default is true: as soon as one matrix job fails, GitHub cancels the other matrix jobs. The name looks like fail fast above, but the fail-fast setting only turns that cancelling on or off.

Branch protection: a PR can't be merged until it's green

A workflow only gives a red or green mark on the PR. To make sure a red PR really can't be merged, add a branch protection rule (a rule that guards a branch) for main:

  1. In your repo on GitHub, open Settings, then Branches (or Rules, then Rulesets).
  2. Turn on Require status checks to pass before merging.
  3. Pick the required checks (a check is the result of one job, shown on the PR), for example lint, test (22), test (24) and e2e.

From then on the merge button stays locked until all those checks pass.

⚠️ Gaps that let things pass CI unnoticed

TL;DR: A CI pipeline is a series of checks that run automatically on every push or PR (pull request), such as lint (a checker for common mistakes in code), tests, then E2E. In GitHub Actions (GitHub's CI service), the pipeline is written as a workflow, a YAML file in .github/workflows/*.yml. on sets when the workflow runs (push and pull_request to main). jobs holds jobs with steps: uses uses an action (a ready-made step), run runs a command. needs orders the jobs, and a job that waits for a failed job is skipped (not run). This is fail fast: the cheap checks go first. strategy.matrix runs the same job on several versions or operating systems in parallel (at the same time), for example Node 22 and 24. Add a branch protection rule (a rule that guards a branch) with "Require status checks to pass before merging" so a PR can't be merged before its checks are green.

⚡ Runs on its own at every push and PR

Write:

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

Meaning: This is part of a GitHub Actions workflow (GitHub Actions is GitHub's CI service), a YAML file in .github/workflows/. Its on section sets when the workflow runs: whenever a commit is pushed to main, and whenever a PR (pull request) to main is opened, reopened or gets a new commit.

⚡ needs: the cheap checks first

Write:

test:
  needs: lint
Guess first: needs: lint makes the job test wait for the job lint (a checker for common mistakes in code) to pass. The job e2e waits for test in the same way. If lint fails, what happens to e2e?

It doesn't run at all, its status is skipped. test is skipped too, because it waits for lint, and e2e waits for test.

Meaning: This order is called fail fast: the cheap, quick checks go first, so the slow E2E run doesn't waste time on code that's surely broken.

⚡ Matrix: one job, several versions

Write:

strategy:
  matrix:
    node-version: [22, 24]

Meaning: strategy.matrix runs the same job for each value in the list, in parallel (at the same time): here test (22) for Node 22 and test (24) for Node 24. Code that only works on Node 24, such as code that uses RegExp.escape, is caught failing on Node 22.

⚡ Branch protection

Meaning: A workflow only gives a red or green mark on the PR. To stop a red PR from being merged (joined into main), add a branch protection rule (a rule that guards a branch) for main, then turn on Require status checks to pass before merging. The merge button then stays locked until every check (the result of each required job) passes.

What you will learn