Sub topic 1
Why Run Tests Automatically
Continuous integration (CI) means every change to a project is built and tested automatically in a shared, clean environment. This chapter explains what that gives you and how GitHub Actions fits in. Later chapters build the workflow step by step.
Prerequisites
This chapter assumes you have:
- Basic Git and GitHub usage: repositories, branches, and pull requests.
- A project with a test command that runs from the terminal, such as
npm test. - Basic familiarity with YAML syntax.
The problem with running tests by hand
Most developers run tests locally before they push. This works until it doesn't. Common failure modes:
- Someone forgets to run the tests, or runs only the tests near their change.
- The code passes on one laptop because of a local setting, an installed tool, or a leftover file.
- Two branches each pass alone but break when merged.
- A reviewer has to guess whether the change was tested at all.
Automatic testing removes these gaps. A server runs the same command on every change, in a fresh environment, and records the result where everyone can see it.
What CI gives you
- Early feedback. A failure shows up minutes after a push, while the change is still fresh in your mind. Fixing it then is cheaper than finding it after release.
- A consistent environment. Each run starts from a clean machine. If the tests need a tool, the workflow must install it, so hidden dependencies surface.
- A shared record. The result is attached to the commit or pull request. Reviewers see it without asking.
- A gate for merging. A later chapter shows how to block merges when tests fail.
CI does not replace good tests. It only runs them reliably. A weak test suite gives you a green check that means little.
How GitHub Actions does it
GitHub Actions is GitHub's built-in automation service. You describe what to run in a YAML file called a workflow, stored in .github/workflows/ in your repository. GitHub watches for events such as a push or a new pull request. When an event matches, it starts a runner, which is a machine that executes your steps, and reports the result back on GitHub.
A first look at the result
Here is the smallest useful workflow for a Node.js project. You will write it properly in the next chapter. For now, just notice how short it is.
name: Tests
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm testThe on section says when to run: on pushes to main and on every pull request. The steps fetch the code, install Node.js 20, install dependencies from the lockfile, and run the tests. If npm test exits with a non-zero status, the step fails and so does the run.
What to expect from the rest of the tutorial
The following chapters add the pieces in order: the workflow file, triggers, jobs and runners, setup, and logs. After that come merge protection, matrices, caching, artifacts, secrets, service containers, permissions, and troubleshooting.
Check your understanding
Exercise
A test suite passes on your laptop but fails in CI. Name two likely causes that automatic testing is designed to reveal.
Show answer
Two likely causes are a dependency or tool that is installed on your laptop but missing from the workflow, and a local setting, environment variable, or leftover file that the clean CI machine does not have. CI starts from a fresh environment, so these hidden assumptions show up as failures.