A first unit test
Understand the idea
A one-line refactor breaks empty carts, but the manual check used a cart with three items. A repeatable boundary test would have caught it.
An executable claim about behavior
A manual check can prove that one run worked, then disappear from memory. A test keeps the input and the expected result so you can repeat the check after a change. That is especially useful for a boundary case you would otherwise forget to try again.
Arrange, act, assert
A useful unit test focuses on a small contract, often a . Arrange the needed data, act by calling the , then assert an observable result. Test names should explain the behavior so a failed run gives you a useful starting point.
Arrange means prepare the input. Act means run the behavior. Assert means compare what happened with what should happen, and fail visibly if they disagree. A unit is a focused piece of behavior, often a function; testing its public result makes the test less tied to how you wrote the internals.
| Jest-style tool | What it expresses | A useful caution |
|---|---|---|
| test(name, callback) | One named behavior to verify | The name should describe the contract |
| expect(actual) | The value being checked | Choose the expected result independently |
| .toBe(expected) | Same-value equality | Separate matching objects do not pass by identity |
| .toEqual(expected) | Supported structural equality | Use when the contents are the contract |
| .toThrow() | A function should throw | Pass a function so the runner can observe the throw |
| await / return a promise | The test must wait for async work | Otherwise an assertion may happen after completion |
Read the Jest-style vocabulary
Jest-style tests use test or it to describe a case and expect with a matcher to assert its result. toBe checks or same-value equality; toEqual checks supported structures. Pick a matcher that expresses the contract instead of converting everything to strings.
A test runner discovers tests, executes them, and reports failures. Jest supplies test and expect along with matchers such as toBe. A matcher is the rule used to judge the result. The live example includes a tiny adapter so this one worked example runs in the browser; it is not a replacement for Jest's full runner.
Choose an independent expected result
Include normal behavior and meaningful boundaries such as an empty collection. A test that repeats the implementation's calculation may repeat its bug too. Prefer small inputs whose expected result you can justify directly. For async work, return or await the so the runner does not finish before the assertion.
Watch a meaningful test fail
Run the test before and after a repair when possible. A test that fails for the intended reason before the change provides stronger evidence than a test that always passes. The browser example below includes a tiny local adapter for test and expect, so it runs here without pretending this iframe is a full Jest runtime. In a Jest project, the runner supplies those two .
The empty-cart expectation is zero because the contract says an empty sum is zero. The two-price expectation is five because you can check that input independently. Removing reduce's initial zero should break the empty case. Seeing a test fail for the intended reason is useful evidence that it is protecting the behavior you care about.
Mental model
FOLLOW THE VALUE
Choose a boundary that matters to the contract.
01 / 03Trace the transformation
Live example
The first test protects the empty-input boundary and the second protects normal addition. Remove the reduce initial value to see the boundary test fail before a false success can be logged.
Your console output appears here.
Guided challenge
Make the boundary test pass
Give reduce an initial accumulator so an empty cart has a valid total.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
PASS empty cart totals zero PASS adds two prices
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.