Test beyond the happy path
Use boundaries, failures, and invariants to design a small set of tests that reveals mistakes in both your own code and AI suggestions.
On this page
A test suite is useful when it can distinguish an implementation that meets the requirements from a plausible one that does not. Several tests with different ordinary inputs may all exercise the same decision. To find more mistakes with a small suite, vary the conditions that change behavior: boundaries, missing data, invalid input, and failed operations.
Start from the contract rather than the current implementation. Otherwise it is easy to write tests that reproduce the code's assumptions, including its mistakes. This matters when you write the code yourself and when an assistant supplies both the implementation and its tests.
Turn requirements into test dimensions
Consider a pure function that calculates the number of pages needed for a collection:
pageCount(total, pageSize) returns at least 1.
total must be a nonnegative safe integer.
pageSize must be a positive safe integer.
Invalid arguments throw RangeError.
For valid arguments, return max(1, ceil(total / pageSize)).
The requirement that an empty collection has one page is a product choice for this example. Another product might use zero. Writing it explicitly prevents a test from depending on an unstated convention.
There are several useful dimensions here: empty versus nonempty collection, exact versus partial final page, valid versus invalid numbers, and small versus large counts. You do not need to test every combination. Choose cases that cross a decision boundary or expose a distinct error.
Test immediately around a boundary
With a page size of 12, the values 11, 12, and 13 are more informative than three arbitrary counts. They distinguish a partial page, an exactly full page, and the first item on a second page.
| Total | Page size | Expected pages | What it checks |
|---|---|---|---|
0 | 12 | 1 | Empty collection convention |
1 | 12 | 1 | Small nonempty collection |
11 | 12 | 1 | Just below a boundary |
12 | 12 | 1 | Exact boundary |
13 | 12 | 2 | Just above a boundary |
24 | 12 | 2 | A later exact boundary |
A mistaken Math.floor would fail the case with 13 items. A formula that always adds one after division would fail the exact boundaries. Naming the mistake a test could catch helps you assess whether another case adds useful coverage.
Write expected results explicitly
A table-driven test keeps these examples readable:
import { expect, it } from "vitest";
it.each([
[0, 12, 1],
[1, 12, 1],
[11, 12, 1],
[12, 12, 1],
[13, 12, 2],
[24, 12, 2],
])("pageCount(%i, %i) returns %i", (total, size, expected) => {
expect(pageCount(total, size)).toBe(expected);
});
Avoid calculating expected with the same formula as the function under test. That can preserve an identical error on both sides of the assertion. For small examples, derive the answer directly: 13 items fill one page of 12 and leave one item for the second page.
Specify invalid input behavior
Test invalid values because the contract names them, not merely because they are imaginable. For this function, useful cases include a negative total, zero page size, fractions, NaN, infinity, and an integer outside the safe range.
it.each([
[-1, 12],
[10, 0],
[1.5, 12],
[10, 2.5],
[NaN, 12],
[10, Infinity],
[Number.MAX_SAFE_INTEGER + 1, 12],
])("rejects pageCount(%s, %s)", (total, size) => {
expect(() => pageCount(total, size)).toThrow(RangeError);
});
Check that the thrown error matches the expected failure contract. If a function promises a result object or fallback value instead, assert that behavior. A test should not pass because an unrelated null access happened to throw.
Keep validation boundaries clear. TypeScript helps callers pass numbers at compile time, but external data can still arrive with the wrong shape. If an HTTP handler accepts unknown input, test its parsing separately from this numeric helper.
Use invariants to cover families of inputs
Examples check particular results. An invariant describes something that should hold across many valid inputs. For positive totals in this example, the returned pages must have enough capacity for all items, while one fewer page must not.
For a fixed positive page size, increasing the total must never reduce the page count. For a fixed total, increasing the page size must never increase the page count. These properties can reveal mistakes across a broader set of inputs when checked with a loop or a property-testing tool.
Keep numeric checks within a range where your test arithmetic is itself reliable. Multiplying very large JavaScript numbers can lose integer precision. A test that accidentally introduces its own rounding issue can make a correct implementation look wrong.
Use a few explicit examples alongside properties. A constant function returning 1 is monotonic, so monotonicity alone cannot establish correctness. Different forms of evidence catch different classes of mistake.
Exercise failures where they actually happen
The page-count helper has no network or storage operations. A page-loading flow does. At that level, test a failed request, an empty response, and a response that arrives after the user changes the filter if the interface supports that interaction.
Assert what the user or caller can observe: an error state, a retry action, preserved input, or the absence of stale results. Avoid tests that merely confirm a private function was called unless that interaction is itself the meaningful contract.
Mocks are useful when a dependency is slow, nondeterministic, or external. Keep them at that boundary, and make the simulated failure resemble the real dependency's behavior. A mock returning an impossible response shape can test a scenario the application will never encounter while missing one it will.
Ask AI for counterexamples, then verify them
An assistant can suggest inputs that might break an implementation. Give it the contract and ask which requirement each proposed case checks. Review the expected outputs yourself before adding the tests.
A useful follow-up is: “What plausible incorrect implementation would still pass this suite?” The answer may reveal a missing boundary or assertion. Treat each suggestion as a hypothesis. You can temporarily make a local, deliberate mistake to confirm that a test detects it, then restore the correct code before committing.
Stop adding cases when they no longer address a distinct risk or requirement. The goal is a suite that makes failures understandable and changes reviewable. Pair those tests with a review of the generated code, and use the same habit during interview practice: state what you expect, gather evidence, and explain what that evidence covers.
Try it in a real coding round.
A little practice makes these ideas your own.