What it is
A test case describes a situation, an action, and an expected outcome. An assertion compares observation with that expectation. The test is only as useful as the behavior its assertions distinguish; executing code alone does not establish correctness.
Why you need it
A test that merely loads a page can pass while its totals are wrong. A clear expected result gives failures meaning and makes coverage discussions concrete.
How it works
- Clarify the behavior and assumptions.
- Arrange a controlled starting state.
- Perform the action.
- Assert the observable result and any important absence of side effects.
A small executable assertion example
import assert from "node:assert/strict";
const eligible = subtotal => subtotal >= 1000;
assert.equal(eligible(999), false);
assert.equal(eligible(1000), true);
assert.equal(eligible(1001), true);Save as discount.mjs and run with Node. These tests demonstrate a specified integer threshold. Real pricing tests must also define currency precision and the exact subtotal calculation.
Good practices and why
- Derive expected results independently from the implementation being tested.
- Assert durable outcomes for critical actions, not just visible success messages.
Common mistakes
- Counting tests as a substitute for identifying risks covered.
- Copying the implementation into the test as its expected-result calculation.
Try it yourself
Write a case for changing a profile name, including the value after a reload and what should happen on a rejected save.
What a successful result looks like
The case names preconditions, action, expected persisted state, and the failure behavior.
0 / 6000 characters · Saved on this device. Do not include passwords or private data.
Check your understanding
Answer the understanding check correctly to record exercise completion.
Related interview practice
These scenarios add constraints and follow-up questions to the concepts you just learned.