Sub topic 15
Enforcing Lint and Format Checks in CI
This chapter assumes you already have the lint and format:check npm scripts from the earlier chapter. It also assumes a project hosted on GitHub with a committed package-lock.json, and that you know basic Git.
Pre-commit hooks catch most problems, but anyone can skip them with git commit --no-verify, and some contributors never install them. Continuous integration (CI) is the check nobody can skip. Every push and pull request runs the same commands on a clean machine, and a failing check can block the merge.
How CI Decides Pass or Fail
CI only reads exit codes. A command that exits with 0 passes and any other code fails the step. Each tool sets its exit code differently:
| Command | Exits non-zero when |
|---|---|
| `eslint .` | At least one rule at `"error"` severity is violated |
| `eslint . --max-warnings 0` | Any error or any warning is reported |
| `prettier . --check` | At least one file differs from Prettier's output |
| `prettier . --write` | Almost never, because it rewrites files instead of failing |
The Workflow File
Save this as .github/workflows/lint.yml:
name: Lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run lint
- run: npm run format:checknpm ci installs exactly the versions in package-lock.json. This matters for Prettier in particular, because its output can change between versions. If you pin Prettier in devDependencies, your laptop and CI format code the same way.
Common Errors
- Warnings pile up unnoticed. ESLint exits with
0on warnings, so CI stays green while warnings accumulate. Add--max-warnings 0to the lint script if warnings should block merges. - Prettier checks build output.
format:checkfails ondist/or generated files. Add those paths to.prettierignore. npm cifails immediately. Either the lockfile is missing or it is out of sync withpackage.json. Runnpm installlocally and commit the updated lockfile.- The check runs but doesn't block anything. In the repository settings, add a branch protection rule (or ruleset) that marks the
lintjob as a required status check.
Exercise
Your lint job passes, but reviewers see dozens of ESLint warnings when they run the linter locally. What change makes CI fail on these warnings, and why does the current setup pass?
Show answer
Change the lint script to eslint . --max-warnings 0. ESLint only exits non-zero for rules at "error" severity, so warnings alone give exit code 0 and CI treats the step as passing.
Exercise
A teammate changes the CI step to npm run format (which runs prettier . --write) so formatting is "fixed automatically." What goes wrong?
Show answer
Prettier rewrites files on the CI runner and exits with 0, so the job passes. The rewritten files are thrown away when the job ends, and the unformatted code gets merged. CI should run prettier . --check.