Sub topic 6
Running the test suite
This chapter covers how a workflow runs your project's test command and how the job's result is decided. It assumes you can write a basic workflow file, know the difference between jobs and steps, and have a project whose tests run from the terminal with one command.
How a test step passes or fails
A run step executes a shell command on the runner. GitHub Actions marks the step by the command's exit code. Exit code 0 means success. Any other code means failure.
Test runners follow this convention. npm test exits with a non-zero code when any test fails, so the step fails and the job stops. Later steps are skipped unless they have an if condition that lets them run.
This means the workflow needs no special logic to detect failing tests. You only need to run the same command you run locally.
A complete test workflow
The workflow below checks out the code, installs Node.js, installs dependencies from the lockfile, and runs the tests.
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 order of steps matters. Checkout must come first, because a fresh runner has no copy of your repository. The language setup must come before any command that needs the toolchain.
Use the install command made for CI
Use npm ci instead of npm install in CI. It installs exactly what package-lock.json lists and fails if the lockfile and package.json disagree. It also deletes any existing node_modules first. This gives you repeatable installs. Other ecosystems have similar commands, such as pip install -r requirements.txt with pinned versions or bundle install --deployment.
Match the local command
Run the same command in CI that developers run locally. If npm test runs a linter, a build, and tests on your machine, it should do the same in CI. Keep the logic in your project's scripts, such as the scripts section of package.json, and call those scripts from the workflow. This avoids drift between what passes locally and what passes in CI.
If you need several commands in one step, use a multi-line run:
- name: Lint and test
run: |
npm run lint
npm testOn Linux and macOS runners, the default shell stops the step at the first command that fails. Separate steps are easier to read in the logs, so prefer them when the commands do different jobs.
Passing options to the test command
You can pass flags to the runner through the script. With npm, put -- before flags meant for the underlying tool:
- run: npm test -- --reporter=dotChoosing a compact reporter keeps logs short. Long logs are harder to scan when a run fails.
Common errors
The table lists frequent causes of a test step that fails in CI but passes locally.
| Symptom | Likely cause |
|---|---|
| `command not found` | The toolchain setup step is missing or comes after the test step |
| `Cannot find module` or missing files | Checkout was skipped, or a dependency exists only in your local `node_modules` |
| Passes locally, fails in CI | Different language version, different operating system, or a missing environment variable |
| Test step never runs | An earlier step failed, or an `if` condition evaluated to false |
| Tests hang until the job times out | A test waits for input or a service that is not running |
A workflow also cannot read files that you did not commit. If a test depends on a local configuration file that is in .gitignore, the file will be missing on the runner.
How the pieces connect
Exercise
A step runs npm test, and two tests fail. The next step uploads a report. By default, does the upload step run, and how would you make it run anyway?
Show answer
By default it does not run, because the failed test step stops the job and skips later steps. Add if: ${{ !cancelled() }} to the upload step so it runs after a failure but not after a cancelled run.
Exercise
Why is npm ci preferred over npm install in a CI workflow?
Show answer
npm ci installs exactly the versions in the lockfile, removes any existing node_modules, and fails if the lockfile and package.json disagree. This makes installs repeatable between runs.