Edge Cases, Boundaries and Table-Driven Tests
Build a working model of automated testing from assertions to unit, component, API, integration, contract and end-to-end tests, then apply it to a single progressive task-board capstone where each milestone adds a real defect, a failing test, a corrected implementation and an expected result.
What you will be able to do
#- Partition inputs into equivalence classes.
- Choose boundary values that expose off-by-one defects.
- Write a table-driven test that covers many cases without duplication.
Overview
#Bugs live at the edges
Most defects are not in the middle of a range. They are at the boundaries: the first element, the last element, the empty input, the maximum value, the value just outside the allowed range. Testing only a typical middle value leaves the boundaries unguarded.
Equivalence partitioning
Split the input domain into classes that should behave the same way. For a function that accepts a percentage from 0 to 100, the classes are below 0, exactly 0, between 1 and 99, exactly 100, and above 100. One representative value per class is enough to catch class-level mistakes. Then add the boundary values themselves, because the transition between classes is where off-by-one errors hide.
Boundary values
For a range [min, max], test min - 1, min, min + 1, max - 1, max, max + 1. For a collection, test empty, one element, two elements and many. For a string, test empty, single character, and multi-byte characters. These six-to-eight cases catch the majority of conditional defects.
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
console.log(clamp(5, 0, 10)); // 5
console.log(clamp(-1, 0, 10)); // 0
console.log(clamp(11, 0, 10)); // 10
Table-driven tests
When many cases share one shape, write them as a table and loop. Each row is input plus expected output. The table documents the covered classes at a glance, and adding a case is one line.
const cases = [
{ input: [-1, 0, 10], expected: 0 },
{ input: [5, 0, 10], expected: 5 },
{ input: [11, 0, 10], expected: 10 },
];
Negative and invalid input
Decide what the contract says about invalid input: throw, return a sentinel, or coerce. Then test that decision. A function that silently returns NaN for bad input is a defect waiting for a user to find it.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What does this print?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.javascriptfunction clamp(value, min, max) { return Math.min(Math.max(value, min), max); } console.log(clamp(-5, 0, 10), clamp(0, 0, 10), clamp(10, 0, 10), clamp(15, 0, 10));Exercise 2Explain
Explain why testing only `clamp(5, 0, 10)` is insufficient.
Reveal the reference answer
reference.javascriptThe middle value only exercises the branch where no clamping happens. It cannot detect a wrong comparison operator, a swapped min and max, or an off-by-one on the boundaries, because none of those paths run. Boundary values at min - 1, min, max and max + 1 exercise the clamping branches where such defects live.The answer should connect the chosen input to the code paths it does and does not execute.
Exercise 3Implement
Implement `inRange(value, min, max)` that returns true when value is within the inclusive range. `console.log(inRange(0, 0, 10), inRange(10, 0, 10), inRange(-1, 0, 10), inRange(11, 0, 10))` should print `true true false false`.
starter.javascriptfunction inRange(value, min, max) { // your code here }Reveal the reference answer
reference.javascriptfunction inRange(value, min, max) { return value >= min && value <= max; }Authored expected output: true true false false
Inclusive bounds require both >= and <=. Using > or < would exclude the boundaries, which the assertions on 0 and 10 detect immediately.
Exercise 4Debug
This `last` helper always returns `undefined` because it indexes one element past the end. Fix it so `console.log(last([]), last([5]))` prints `undefined 5`.
starter.javascriptfunction last(items) { return items[items.length]; } console.log(last([]), last([5]));Reveal the reference answer
reference.javascriptfunction last(items) { if (items.length === 0) return undefined; return items[items.length - 1]; } console.log(last([]), last([5]));Authored expected output: undefined 5
items[items.length] is always one past the end, so it is undefined for every array and never the last element. The fix indexes length - 1 and returns undefined explicitly for the empty array instead of relying on the out-of-bounds read.
Exercise 5Test
Write a table-driven test for `inRange` covering below, on-min, inside, on-max and above.
starter.javascriptfunction inRange(value, min, max) { return value >= min && value <= max; }Reveal the reference answer
reference.javascriptfunction assertEqual(actual, expected, label) { if (actual !== expected) throw new Error(label + ': expected ' + expected + ' but got ' + actual); } const cases = [ { value: -1, expected: false }, { value: 0, expected: true }, { value: 5, expected: true }, { value: 10, expected: true }, { value: 11, expected: false }, ]; for (const c of cases) assertEqual(inRange(c.value, 0, 10), c.expected, 'inRange(' + c.value + ')'); console.log('inRange table verified');Authored expected output: inRange table verified
The table lists one row per equivalence class and boundary. The loop runs every row through the same assertion, so the coverage is visible and adding a case is a single line.
Exercise 6Interview
In an interview, walk through how you choose test cases for a function that paginates a list.
Reveal the reference answer
reference.javascriptI start from the contract: page size, total items and the requested page. I partition into empty list, list shorter than one page, exactly one page, exactly two pages, and a partial final page. I add boundaries: page 1, the last page, a page beyond the end, and page 0 or negative. I also check the off-by-one between the last item of a page and the first of the next. Then I write them as a table so the coverage is explicit.The answer should demonstrate partitioning plus boundary selection and should mention the empty and beyond-the-end cases.
Common misconceptions
#- Testing the happy path is enough if the code looks simple.
- Boundary testing means only testing the maximum value.
- A table-driven test is less rigorous than separate tests.
Follow-up practice
#- testing-error-and-negative-paths
- testing-async-and-timers
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.