Skip to content
engineering workbench.by StudyPort Academy
Learning path
Real-World20 minLESSON 34 / 34

A first unit test

01

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.

The vocabulary of a Jest-style test
Jest-style toolWhat it expressesA useful caution
test(name, callback)One named behavior to verifyThe name should describe the contract
expect(actual)The value being checkedChoose the expected result independently
.toBe(expected)Same-value equalitySeparate matching objects do not pass by identity
.toEqual(expected)Supported structural equalityUse when the contents are the contract
.toThrow()A function should throwPass a function so the runner can observe the throw
await / return a promiseThe test must wait for async workOtherwise 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.

02

Mental model

FOLLOW THE VALUE

arrange input
empty cart
[]

Choose a boundary that matters to the contract.

01 / 03Trace the transformation

03

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.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Make the boundary test pass

Give reduce an initial accumulator so an empty cart has a valid total.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

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).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Which expected value is best for subtotal([2, 3])?
    02Why test an empty array?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress