CI tests
One npm script
One command must run the whole suite. A test script in package.json compiles the project and runs the compiled tests, so every machine repeats the same steps with npm test. No tribal knowledge, no wiki page with five manual steps:
{ "scripts": { "test": "tsc -p tsconfig.json && node dist/tests/math.test.js" } }
The package has a name and a version as well, because npm prints both above the command it runs. The project of this part is ts-cart at version 1.0.0.
npm test
> ts-cart@1.0.0 test
> tsc -p tsconfig.json && node dist/tests/math.test.js
pass: doubles two
Local runs and server runs now execute identical steps. When a test fails on the server but passes locally, the cause is the environment, not a forgotten flag.
Non-zero fails
A failing test must fail the command. An assert that throws makes Node exit with a non-zero code, and npm passes that code through, so npm test turns red exactly when a check breaks. Never silence this chain with fallback tricks or empty catch blocks around asserts:
npm test
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
4 !== 5
That block in the log names the broken expectation. The fix lands in the product code or in the test, and the next run decides the verdict.
Node versions note
Note the Node versions the project supports, and run the suite on each of them. A short support note in the readme names them, plus one server run per version. New syntax that older Node cannot parse is the usual surprise, and the matrix catches it before users do.
Keep the list short: the current LTS and the previous one cover almost every reader. Drop old versions openly with a note, instead of letting them rot in silence.
No merge on red
Make green tests a merge rule, not a suggestion. No branch merges while the suite is red: the rule is simple and applies to everyone, including urgent fixes. Red means stop, fix forward, and merge only when the run turns green again.
The rule protects the main branch around the clock. Every commit stays releasable, because no broken change ever lands on top of it.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the point of one
testscript in package.json?Answer
Every machine and server runs the same steps with one command
The script records the compile and the run once, so a local run and a CI run cannot drift apart.
-
A test throws an
AssertionError. What doesnpm testdo?Answer
It prints the error and exits with a non-zero status
Node exits non-zero on an uncaught error, and npm passes that status through, so CI turns red.
-
Why run the suite on the current LTS and the previous one?
Answer
New syntax that an older Node cannot parse is caught before users meet it
The version matrix is the cheap way to find syntax and API differences between the versions you support.
-
What does "no merge on red" mean?
Answer
A branch merges only when the suite is green
Green tests are a merge rule, so no broken change lands on the main branch.
-
What does this program print?
import assert from "node:assert/strict"; assert.equal(2 + 2, 4); console.log("green");Answer
green
The assertion passes, so the next line runs and prints green.
-
What happens when this program runs?
import assert from "node:assert/strict"; assert.equal(2 + 2, 5); console.log("green");Answer
It prints nothing: the assert throws an AssertionError before the log
The mismatch throws at once, so the log line never runs and the process exits non-zero.
-
What does this program print, and with what status does it exit?
process.exitCode = 1; console.log("done");Answer
done / status: 1
The line prints, and the exit code set on process becomes the status Node returns when it finishes.
-
Why does an empty
catcharound an assert break the chain?Answer
It hides the failure, so npm test stays green while the check is broken
A swallowed assertion never reaches Node, so the command exits 0 and CI cannot see the broken check.
-
What is the difference between
tsc -p tsconfig.jsonandtsc tests/math.test.ts?Answer
The first reads the project settings, the second ignores the tsconfig file
A file named on the command line makes tsc ignore tsconfig.json, so the project flags do not apply.
-
What does npm print above the output of a script when it runs
npm test?Answer
The package name and version, then the command it runs
npm prints a two-line banner with the package name at its version and the script command before the output.
Article author: Arthur Isaev