Skip to main content

Two Ways to Fail

A test can report a problem in two ways, and the difference is what happens next:

Every assertion in node:assert/strict throws when it fails. A throw ends the test it is in, so the checks written after it never run. That is fail-fast behavior, and it is what you get by default.

To report a problem and keep checking, put each check in its own test(…) call. Tests are independent: one failing test does not stop the next one, so a single run can report several problems at once.

Both shapes take a message. assert.equal(got, want, "…") and assert.ok(value, "…") print your text in place of the default one.


Separate Tests: Report and Continue

When you want to check several cases in a row, give each case its own test. A broken case is reported, and the cases after it are still checked.

That way one run shows you the whole damage — not just the first broken case.

See every broken case at once

This add ignores b, so all three checks fail. Because each check is its own test, all three ✖ lines appear in a single run and the summary says fail 3. Written as three checks inside one test, only the first would have been reported.

Prompt
Write a TypeScript test file where add has a bug (it returns only a, ignoring b) and three checks live in three separate tests, so every broken case is reported. Show the real output with all three failures in one run.
Output: TypeScript file and captured output

A Failed Assertion Stops the Test

Sometimes a failed check means the rest of the test cannot work. A typical case: the test first prepares a value, then inspects it. If the preparation already went wrong, inspecting it would be useless or would even crash.

Here the throwing assertion is exactly what you want. It ends the test right there, and the checks below it never run — and that is the point.

Stop before the useless checks

splitWords has a bug (it splits on the wrong character), so 1 part comes back instead of 3. The length check fails and throws, so the check of the first part below is never reached — the report shows that one failure and nothing else.

Prompt
Write a TypeScript test file where a splitWords function should split a comma-separated string but has a bug (it splits on the wrong character). The test asserts the number of parts first and the first part after. Show the output where the failed assertion stops the test before the second check.
Output: TypeScript file and captured output
Stop before the useless checks

Messages on Boolean Checks

assert.ok(value, message) is the check for a plain boolean: it fails when the value is falsy. Use it where there is no got and want pair to compare, only a yes-or-no answer.

The message is the second argument, and it is optional. Without it the runner still prints its own text and the values it compared, so your own message is worth writing when it says more than that.

Plain messages

Two simple boolean checks, each in its own test, each with a plain message. Both pass here, so the messages stay silent and the summary says pass 2.

Prompt
Write a TypeScript test file with an isEven function and two tests that check isEven(4) is true and isEven(3) is false using assert.ok with a plain message. Show the passing output.
Output: TypeScript file and captured output
Plain messages