Exercise: Lost and Found CI
Add ESLint and Prettier to the Lost and Found Desk's API with npm scripts, fix every problem they report without changing what the API answers, and add a GitHub Actions workflow that lints, checks the format and runs the tests on every push.
Journeyman · Node.js · GitHub Actions · Software Development · Updated
The Lost and Found Desk's API works, and its tests pass. But three students write it, each in their own style, a few likely mistakes have crept in, and nothing checks a push before it lands on GitHub. You'll give the project the same setup a team would: ESLint and Prettier with npm scripts, every problem they report fixed, and a GitHub Actions workflow that runs every check on every push. When you push, GitHub shows your first green run.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-node-lost-and-found-ci, also linked from the Exercise box beside the lesson. Clone it into your nodejs-course folder. In VS Code's terminal:
cd ~/nodejs-course
git clone https://github.com/ask-codey/exercise-node-lost-and-found-ci.git
Git makes a folder, exercise-node-lost-and-found-ci, with the exercise code inside. Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and install the packages:
npm install
ESLint, Prettier and the rest are already in package.json at exact versions, so npm install brings them all. Get the Exercise Code with Git walks through cloning step by step, if you'd like a reminder.
The folder holds:
app.js: the API, for you to clean up. The comment at its top lists every request it answers.items.js: the found items and the claims, kept in memory, for you to clean up too.server.js: starts the API athttp://localhost:3100.lost-and-found.http: the requests to try with REST Client.app.test.js: the API's own 9 tests, which pass already and must still pass at the end.quality.test.js: the exercise's 12 checks, on your scripts, your ESLint config, your files and your workflow. You don't need to change either test file.
What to do
- Add three scripts to
package.json, besidestart,devandtest:"lint": "eslint .","format": "prettier --write ."and"format:check": "prettier --check .". - Make
eslint.config.jsat the top of the folder: a flat config withjs.configs.recommended,languageOptions: { globals: globals.node }, and three rules:eqeqeq: "error","prefer-const": "error"and"no-unused-vars": ["error", { argsIgnorePattern: "^_" }]. - Run
npm run lintand fix all six problems it reports inapp.jsanditems.js. Change the code; never switch a rule off or add a disable comment. Runnpm testafter each fix: the API's tests must stay green. - Run
npm run format, so Prettier lays out every file the same way. - Make
.github/workflows/ci.yml(the folder.github, the folderworkflowsinside it, then the file): a workflow namedCIthat runson: [push, pull_request], with one job thatruns-on: ubuntu-latest. Its steps, in this order:uses: actions/checkout@v7;uses: actions/setup-node@v7withnode-version: 26andcache: npm; thenrun: npm ci,run: npm run lint,run: npm run format:checkandrun: npm test. Runnpm run formatagain afterwards: Prettier formats YAML too.
Check your work
npm test
It runs both test files, and no server needs to be running. At first the API's 9 tests (API 1) to API 9)) pass and the 12 checks (1) to 12)) fail: that's expected, because nothing is set up yet. Each part you finish turns its checks green. A failing check says what's missing, for example Add "lint": "eslint ." to the scripts in package.json. Check 7 lists every problem ESLint still reports, with its file, line and column, and check 9 names the line of ci.yml that YAML couldn't read.
Hints
- A route parameter is text. If
API 3)turns red after you change a==to===, the id from the address is"2"and the item's id is2. ESLint and Prettier for a Node Project shows the fix:Number(id). - The error handler needs all four parameters. Express knows it by them, so
nextstays: rename it_next, which yourno-unused-varssetting skips. - An import nothing uses can go, line and all. A
letthat never gets a new value becomesconst. - Copy the steps from the lesson. Tests on Every Push with GitHub Actions has the whole workflow, line by line. Indent with two spaces, never tabs.
- Run the steps yourself before you push:
npm ci,npm run lint,npm run format:check,npm test. If they pass on your computer, they pass on GitHub's.
When you're done
Every test is green, and npm run lint and npm run format:check print no problems. The end of npm test:
✔ 12) Then the steps run npm ci, npm run lint, npm run format:check and npm test, in that order (0.9482ms)
ℹ tests 21
ℹ suites 0
ℹ pass 21
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 615.4843
The last check happens on GitHub. When you push in Submit your exercise below, open your repository's Actions tab: a run of CI starts within seconds, and after a minute or so its job lint-and-test shows a green check beside every step, from checkout to npm test. If a step is red, open it and read its log; it's the same output you'd see on your computer.
Then add the status badge at the top of README.md, with your GitHub name and repository in the address, commit and push again:

Look back at the six problems you fixed: which of them would have broken the API if someone had "fixed" it by deleting a line or switching a rule off, and which test would have told you?
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.
On GitHub, click +, then New repository. Name it (for example
exercise-node-lost-and-found-ci), choose Public, and click Create repository. Leave out the README: your folder already has one.In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-node-lost-and-found-ci.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-node-lost-and-found-ci, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
This is how a team's repository looks from the first day of a job: a linter and a formatter that everyone runs the same way, and a pipeline that checks every push, so a mistake shows up as a red cross in minutes instead of a bug report days later.