1. Checking by hand is a dead end
— Hey, — Tommy writes, — so how do I prove anything at all? Well, I’ll fix it. I’ll open the game, make three in a row, and look at the scoreboard. If it shows three, it means it’s fine.
— And what if there are five bugs? — asks Lyoha. — And ten zones in the game? Will you manually click through each one after every change? With just this one bug, how many times did you restart the match before you caught it?
Tommy thinks it over. Five times, for sure. Build it, check it, don’t believe it, again, again.
— Well, about five times.
— Exactly. Five runs for one bug. And there are dozens of changes in a day. If you check everything by hand after every change, you’ll go crazy and still miss something. You’ll get tired, forget, let something slip. People got burned by this until they came up with a simple idea: let the machine check for you.
2. A test is a tiny checker
— It’s called a test, — Lyoha continues. — Don’t let the word scare you. A test is a small program with one job: take your code, give it a known example, and check whether the answer came out right. You describe once what counts as correct, and then the machine runs it by itself, even a hundred times a second, and it does not get tired or forget.
— You mean a program checks a program?
— Exactly. Look, I’ll show you a test for your score bug. Don’t be scared that it’s code — it reads like plain English:
test("three in a row gives 3 points", () => {
if (getScore(3) !== 3) {
return "ERROR: points calculated incorrectly";
}
return "all good";
});
— Read it as plain words, — Lyoha says, tracing the lines. — “A check called ‘three in a row gives 3 points.’ Count the points for three in a row — that’s getScore(3). If the amount is not equal to three, shout ‘ERROR.’ Otherwise, say ‘all good.’” That’s it. That’s the whole test.
Tommy reads it again, moving his lips. getScore(3) — count the points for a trio. !== 3 — “not equal to three.” If it’s not three, it’s an error.
— So this is… simple, — he says. — You don’t even have to be a programmer. “Count it. If it’s not three — yell Errorrr!”
— And that’s the whole point, — Lyoha nods. — A test is a written rule for “how it should be,” plus a shout if it turns out differently. Right now you have a bug: three gives thirty. So getScore(3) will return thirty. Thirty is not equal to three — the test will yell “ERROR.” That’s how it catches your bug without opening the game or clicking around by hand.
3. How you run a test
— And how do I… run it? — Tommy looks for the button.
— With one command. In a project it’s usually the command npm test — “run all tests.” You type it, and then the machine takes each test, runs it, and reports back. Look how the report looks when a test finds your bug:
FAIL score.test.js
● three in a row gives 3 points
expected: 3
got: 30
— FAIL means “failed,” in red. — Lyoha points at the lines. — The test honestly reports back: expected three, got thirty. It didn’t match — so it yelled. And here’s how it looks when the bug is fixed and the score is correct:
PASS score.test.js
✓ three in a row gives 3 points
— PASS — “passed,” in green. It counted, it matched the three, it stayed quiet and was happy. That’s the whole alphabet: red FAIL — the check caught something, green PASS — everything matched.
Tommy nods — he had already seen that red-and-green pair in passing, but now he understands where it comes from.
4. What tests actually check
— And last, — says Lyoha. — Tests do not cover everything under the sun; they cover the things where a precise result matters and where it is easy to make a mistake without noticing. Score counting, reward calculation, game rules — that kind of thing. You give input, you know the correct answer — check it with a test.
— And what do they not cover?
— Things that are easier to check with your eyes than with a number. Whether the buttons are laid out nicely, whether the color is pleasant, whether the animation is smooth — you don’t hand that to a test; a person looks at it. A test is strong where there is a right answer you can compare against. Your score is exactly that kind of case: three in a row MUST give three. There is something to compare with — there is something to test.
Tommy closes the example, and a simple picture settles in his head: a test is a machine-checker that you told once, “this is what right looks like,” and it keeps watch over that instead of you. Not magic. A written rule and a shout if the rule was broken.