Start learning free

LESSON 155 OF 173 · BACKEND: SHIPPING IT

Run the Tests on Every Push

Practice

Continuous integration (CI) means your tests run by themselves every time you push. On GitHub you ask for it with a workflow: a YAML file in the folder .github/workflows/, made of key: value lines nested by their indentation. The key on says which event starts it, and each job runs its steps from top to bottom on a fresh machine, the runner.

Here is a small workflow of another kind. It starts on every pull request and runs one command:

name: Hello
on: pull_request
jobs:
  greet:
    runs-on: ubuntu-latest
    steps:
      - run: echo Hello from the runner

A step either uses a ready-made action, such as actions/checkout, which copies your repository onto the runner, or runs a command, as you would type it in a terminal. The runner starts with a Node.js of its own and nothing of yours: no code, no Express. So the order matters: copy the code, set up the server's Node.js, install the packages with npm ci, then run the tests with node --test, which runs app.test.js, the JavaScript that tsc compiles from the shop's app.test.ts. The job stops at the first step that fails.

You write the workflow as the string workflow: an array with one string per line of the file, joined with \n (a string[] made into one string, as in the Dockerfile). The check reads it the way GitHub would, runs the job and must print:

Workflow: Tests
Starts on: push
Job test, on a fresh ubuntu-latest machine
Step 1: uses actions/checkout@v6
  copied the repository: app.js, package.json, package-lock.json, app.test.js
Step 2: uses actions/setup-node@v6 with node-version 22
  Node.js 22 is ready
Step 3: run npm ci
  installed express 5.1.0 and the packages it needs, in the exact versions written in package-lock.json
Step 4: run node --test
  tests 3, pass 3, fail 0
The job passed: every push now runs the tests on Node.js 22.

Your task: the starting workflow already has its name and the job test on ubuntu-latest. Add the trigger on: push and four steps: check out the code with actions/checkout@v6, set up Node.js '22' with actions/setup-node@v6, install the packages with npm ci, and run the tests with node --test.


Assignment
Follow the Coding Tutorial to write a GitHub Actions workflow that runs the shop's tests on Node.js 22 on every push.


Hint
Indentation is the structure, and it must be spaces: the step items start 6 spaces in with a dash, with: sits 8 spaces in under its uses, and node-version 10. Write the version in quotes, '22', so YAML keeps it as text. Each line is its own string in the array, with a comma after it.


STEP 1 OF 7
    Ctrl + Enter to check