Linting, Formatting and a Test Runner as Guardrails
Set up an ESLint flat config, Prettier and Vitest as three npm scripts that catch real defects, and see exactly what each guardrail reports when it fires.
Contentsand lesson path
On this page
- The task
- ESLint and the flat config
- Prettier and the format check
- Vitest and the test script
- One script that fails as a unit
- Check your understanding
- Apply it
- Review prompt
Lessons of Frontend Tooling & Builds
- Packages, Versions and Lockfiles Concept · 20 min
- Inside a Vite Project: Dev Server, Production Build and Composed Apps Worked example · 30 min
- Linting, Formatting and a Test Runner as Guardrails Worked example · 20 min
- Lab: From Empty Folder to a Linted, Tested, Built Project Lab · 30 min
- Pairing with an AI Coding Assistant: Context, Review and Verification Worked example · 25 min
- Check: Read a package.json, a Build Report and an AI-Generated Diff Knowledge check · 8 questions
What you will learn
- Enforce code quality with an ESLint flat config, Prettier and a Vitest test script, wired into npm scripts so a failing check blocks a commit or merge.
01. The task
The list project now builds (last lesson); the next step is making bad changes hard to merge. Three tools divide that work:
- ESLint reads your code without running it and reports rules it breaks: unused variables, unreachable branches, suspicious comparisons.
- Prettier rewrites your files into one agreed format, so once everyone runs it, formatting differences stop showing up in diffs.
- Vitest runs the tests you wrote for the pure
list.jsmodule in isolated worker processes (child processes, Vitest's defaultforkspool), reusing Vite's transform pipeline.
The important design rule: a guardrail only protects the project where a failure is enforced — a red CI run or a blocked merge, not a squiggle someone can ignore. So this lesson wires all three into npm scripts and ends with one check script that fails as a unit. The runs below are real, from the same list project, with the versions the lab pins: Vite 8.4.0, ESLint 10.15.0, Prettier 3.10.1 and Vitest 5.1.2.
02. ESLint and the flat config
ESLint 10 reads a single configuration file written as an array. Each entry is a set of settings that applies to matching files; ESLint merges them in order for every file it checks. The recommended core comes from the @eslint/js package, and two extra rules turn it into the gate the practice needs — most notably no-unused-vars as an error:
import js from '@eslint/js';
import globals from 'globals';
export default [
js.configs.recommended,
{
languageOptions: {
globals: globals.browser,
},
rules: {
'no-unused-vars': 'error',
eqeqeq: 'error',
},
},
{
ignores: ['dist/'],
},
];The first lint run on this project found four problems — and every one teaches something:
$ npm run lint
src/main.js
9:14 error 'document' is not defined no-undef
10:21 error 'document' is not defined no-undef
16:18 error 'document' is not defined no-undef
src/sum.js
6:9 error 'doubled' is assigned a value but never used no-unused-vars
✖ 4 problems (4 errors, 0 warnings)The three document errors are a configuration gap, not a code defect: ESLint runs in Node, where document does not exist, and it refuses to guess which globals your code may use. The globals package lists the globals of each environment, and globals.browser tells ESLint this code runs in a web browser (the DOM work made that true). One config entry later, only the real defect remains:
$ npm run lint
src/sum.js
6:9 error 'doubled' is assigned a value but never used no-unused-vars
✖ 1 problem (1 error, 0 warnings)And that defect is worth the rule. In sum.js, a helper computing a total, line 6 read const doubled = total * 2; — a leftover experiment. The variable was computed and thrown away, which usually means a forgotten return or a rename that abandoned a step. Deleting the line and re-running the lint leaves ESLint silent and, like most lint and test commands, exiting 0 when nothing is found. A rule you never saw fire is a rule you will not trust; this one paid for itself on its first run.
03. Prettier and the format check
Prettier needs almost no configuration — its power is that it makes the decisions, not you. This project fixes two options in .prettierrc.json: a print width of 88 characters and single quotes. Then prettier --check . compares every file against what Prettier would write, and a file saved by hand with different spacing shows up immediately:
$ npm run format:check
Checking formatting...
[warn] src/summary.js
[warn] Code style issues found in the above file. Run Prettier with --write to fix.
$ echo $?
1The offending file, as written by hand and as prettier --write . leaves it:
import { applyFilter } from "./list.js";
export function summary(items, query) {
const visible = applyFilter(items, query);
const labels = visible.map(function (item) { return item.label; });
return labels.join(", ");
}import { applyFilter } from './list.js';
export function summary(items, query) {
const visible = applyFilter(items, query);
const labels = visible.map(function (item) {
return item.label;
});
return labels.join(', ');
}Notice what changed: quotes, the multi-line formatting of the function expression, nothing else. The workflow is deliberate — --write for developers (usually wired to a save action in the editor), --check for CI, because CI must report rather than rewrite. ESLint and Prettier stay out of each other's way by splitting the job: ESLint owns rules about correctness, Prettier owns formatting. If a formatter choice ever collides with an ESLint style rule, the standard fix is eslint-config-prettier, which switches the overlapping ESLint rules off rather than making the two tools fight.
04. Vitest and the test script
Vitest looks for files matching test patterns such as *.test.js and runs them. Because it reuses Vite's transform pipeline, the imports resolve the same way as in the Vite dev server and build. list.test.js tests the pure filtering function — no browser, no DOM, milliseconds per run:
$ npm run test
RUN v5.1.2 <project>
Test Files 1 passed (1)
Tests 3 passed (3)The script says vitest run, not just vitest. Without run, Vitest picks a mode for you: in an interactive terminal it starts watch mode, which runs the tests and then waits for file changes — ideal while editing, but a command that does not finish. It runs once only when it detects CI (the CI environment variable) or no interactive terminal. Writing run removes the guess: the command runs once and exits non-zero if any test fails, wherever it is started, which is exactly the contract a gate needs.
05. One script that fails as a unit
Each tool reports failure the same way: a non-zero exit code. That makes them composable with &&, which runs the next command only when the previous one exits 0. One script now chains all three gates:
"lint": "eslint .",
"format:check": "prettier --check .",
"test": "vitest run",
"check": "npm run lint && npm run format:check && npm run test"npm run check now has exactly two outcomes: it prints three passing reports and exits 0, or it stops at the first failing tool and exits non-zero. Wire it into CI as a required step and a red check blocks the merge — see pull request reviews for where that gate sits in the workflow. Fixing a failure is mechanical, because the failing command names itself: lint errors say the file, line and rule; Prettier names the file; Vitest prints the failing test's assertion.
06. Check your understanding
07. Apply it
On the shopping-list practice for this skill:
- Copy the config from this lesson, run
npm run lint, and fix every error it reports — including declaring your environment's globals. - Deliberately leave one unused variable in a scratch file, run
npm run lint, read the report, then remove it and watch the exit code change. - Add the
checkscript, break the formatting of one file by hand, and runnpm run checkto see the chain stop at the format gate before any test runs.
08. Review prompt
Without looking back: what does each of the three tools own, and what does each report when it fails? Why does CI run --check while developers run --write? Why vitest run and not vitest? And what makes npm run check fail as a unit?
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.