All the checks in one script

A project ends up with four commands that nobody remembers in the right order: the linter, the format check, the tests and the build. One script that runs them in that order turns the question into a single word, and its exit status is the answer a pipeline, a pre-commit hook and a colleague all read.

The lesson builds the script out of the three tools of this part, runs it once with everything in order and once with a broken test, then replaces the chain with a small runner that runs every step and prints a table instead of stopping at the first failure.

Four scripts and one chain

Each check has a script of its own, so it can be run alone while the code is written, and the check script joins them with &&. The operator runs the next command only when the previous one exited with 0, which is why the chain stops at the first failure and why its status is the status of the step that failed.

Every step is called through npm run --silent, so the header lines of npm do not appear four times in a row, and the last one prints a line that a reader can recognise.

The file package.json:

vi package.json

{ "name": "check-course", "version": "1.0.0", "type": "commonjs", "scripts": { "lint": "eslint .", "format": "prettier --check . --log-level warn", "test": "node test/sum.js", "check": "npm run --silent lint && npm run --silent format && npm run --silent test && node done.js" }, "devDependencies": { "@eslint/js": "^10.0.1", "eslint": "^10.14.0", "prettier": "^3.9.10" } }

The test file is a plain runner of three cases: it prints a line for a failure, a line with the count and sets the exit code. The lesson on node:test shows the other shape, where the runner of the runtime does the same work.

The file test/sum.js:

const { sum } = require('../src/sum'); const cases = [ { input: [1, 2, 3], expected: 6 }, { input: [], expected: 0 }, { input: [-1, 1], expected: 0 }, ]; let failed = 0; for (const { input, expected } of cases) { const actual = sum(input); if (actual !== expected) { failed += 1; console.log('failed: sum(' + JSON.stringify(input) + ') was ' + actual + ', expected ' + expected); } } console.log(cases.length - failed + ' of ' + cases.length + ' tests passed'); process.exitCode = failed === 0 ? 0 : 1;

The file done.js:

console.log('all three checks passed');

A run where everything passes

The linter prints nothing when the folder is clean and the format check prints nothing with the warn log level, so the output of a good run is the two lines of the test and the last program.

npm run --silent check
echo "the exit status: $?"

3 of 3 tests passed
all three checks passed
the exit status: 0

Three silent tools and two lines of output, which is the shape a check should have: the noise of a check is the problem rather than the work. A build server runs exactly this command, and a pre-commit hook can call npm run check without knowing what is inside.

A run that stops

One bug in the function is enough. The body of sum takes the absolute value of every number, so the two positive cases still pass and the third one does not.

The file src/sum.js:

function sum(values) { return values.reduce((total, value) => total + Math.abs(value), 0); } module.exports = { sum };

npm run --silent check
echo "the exit status: $?"

failed: sum([-1,1]) was 2, expected 0
2 of 3 tests passed
the exit status: 1

The failing case names the input, the result and the expectation, which is everything a reader needs before opening the file. The lint and the format steps ran before the test and passed, so the chain reached the end and the status is the status of the test program.

The order is not arbitrary: the linter is the fastest and catches the errors that make the other two useless, the format check is next because it never needs a correct program, and the tests are last because they are the slowest and the most informative. A chain that put the tests first would spend seconds on a file with a syntax error.

A runner that runs every step

Stopping at the first failure is right for a commit and wrong for a first look at an inherited project, where the reader wants all the problems in one run. A small program can run the three scripts, keep every status and print one table.

The file run-all.js:

const { spawnSync } = require('node:child_process'); const steps = ['lint', 'format', 'test']; let failed = 0; for (const step of steps) { const run = spawnSync('npm', ['run', '--silent', step], { encoding: 'utf8' }); const passed = run.status === 0; if (!passed) { failed += 1; } console.log(step + ': ' + (passed ? 'passed' : 'failed')); if (!passed) { for (const line of run.stdout.trim().split('\n')) { console.log(' ' + line); } } } console.log(failed + ' of ' + steps.length + ' step(s) failed'); process.exitCode = failed === 0 ? 0 : 1;

node run-all.js
echo "the exit status: $?"

lint: passed
format: passed
test: failed
  failed: sum([-1,1]) was 2, expected 0
  2 of 3 tests passed
1 of 3 step(s) failed
the exit status: 1

The same failing test, and the difference is what the reader sees: one line per step whether it passed or not, the output of the failing step indented under its name, and the status 1 at the end because one step failed. spawnSync gives the status and the two streams separately, which is why the program can decide what to print. On Windows the call needs shell: true, because npm is a script rather than an executable there.

Exercises

  1. Break a semicolon and run npm run check. Which step reports it, and which two steps never run?
  2. Replace && with ; in the check script and break a file. Which steps run now, and what is the status of the chain?
  3. Add a fourth step that prints the version of the package and run the chain. Which line of the output belongs to it?
  4. Change run-all.js to stop at the first failure. Which lines of the table disappear, and what does the program print instead?
  5. Make the test program exit with 2 instead of 1 when a case fails. Which numbers change in the output of the chain, and which one does not?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does && do between two commands in a script?

    Answer

    It runs the second only when the first exited with 0

    The chain stops at the first command that fails, and its status becomes the status of the chain.

  2. What does this program print? const steps = ["lint", "format", "test"]; console.log(steps.slice(0, 2).join(" then "));

    Answer

    lint then format

    The slice keeps two entries and the join puts the words between them.

  3. Why are the tests the last step of the chain?

    Answer

    They are the slowest, and a syntax error makes them useless

    The cheap checks come first so that the expensive one runs only on code that already passes them.

  4. What does this program print? const run = { status: 1, stdout: "failed: one case\n" }; console.log(run.status === 0 ? "passed" : "failed");

    Answer

    failed

    The status is not zero, so the conditional picks the second string.

  5. What is the exit status of a chain whose third of four steps fails?

    Answer

    The status of the third step

    The chain stops at the failing command, and the shell returns that command's status.

  6. What does this program print? const counts = { passed: 2, failed: 1 }; console.log(counts.passed + counts.failed, counts.failed);

    Answer

    3 1

    The sum of the counts is three and the second argument is the number of failures.

  7. What does a chain of silent checks print when every step passes?

    Answer

    Only the lines of the steps that print something

    The linter and the format check are silent on success, and the programs that print still print.

  8. What does this program print? const steps = ["lint", "format", "test"]; let failed = 0; for (const step of steps) { if (step === "test") { failed += 1; } } console.log(failed + " of " + steps.length);

    Answer

    1 of 3

    Only the last step matches and the counter ends at one.

  9. What does spawnSync give the runner that a chain of && cannot?

    Answer

    The status and the output of every step, even after a failure

    The runner reads the child process result itself, so it can continue and report a table.

  10. What does this program print? const ok = [true, true, false]; console.log(ok.every((value) => value), ok.some((value) => !value));

    Answer

    false true

    Not every value is true, and at least one value is false.

The programs and commands on this page were run with Node.js 24.22.1, npm 11.12.1, ESLint 10.14.0 and Prettier 3.9.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev