Test pure component logic
Choose the smallest test that proves the contract
A test is an executable claim about behavior. It arranges an input, performs an action, and checks an expected outcome. Unit tests isolate a small piece of logic; integration tests connect multiple pieces; component tests exercise a rendered interface; end-to-end tests exercise a larger workflow in a real browser. These categories overlap. Prefer many fast checks of business rules and a smaller number of broader checks for wiring, accessibility and navigation. The right balance depends on the risks, not a fixed ratio. This chapter uses an original seed library reservation feature. A visitor can reserve up to the number of packets available. Separate that rule from React so we can execute it with Node without a DOM.
export function reserve(state, amount) { if (!Number.isSafeInteger(amount) || amount < 1 || amount > state.available) return state; return { ...state, available: state.available - amount, reserved: state.reserved + amount };}Given three available and zero reserved, requesting two returns a new object with one available and two reserved. The old object remains unchanged. Invalid requests return the original object. This fixture assumes nonnegative safe-integer counts and a small total established when data enters the application; production validation must enforce the invariant at that boundary. The rule has no browser, network or time dependency. It is not a real stock reservation: the backend must independently authorize and atomically enforce inventory limits.
Test normal, boundary and invalid cases
import { test } from 'node:test';import assert from 'node:assert/strict';import { reserve } from './reservation.mjs';const initial = () => Object.freeze({ available: 3, reserved: 0 });test('reserves without mutating input', () => { const before = initial(); const after = reserve(before, 2); assert.deepEqual(after, { available: 1, reserved: 2 }); assert.deepEqual(before, { available: 3, reserved: 0 }); assert.notEqual(after, before);});test('allows exact stock', () => assert.equal(reserve(initial(), 3).available, 0));for (const amount of [0, -1, 4, 1.5, NaN, '2']) { test(`rejects ${typeof amount}:${amount}`, () => { const before = initial(); assert.equal(reserve(before, amount), before); });}Run node --test reserve.test.mjs in the fixture directory. Each test receives a new frozen input; a failing assertion causes a nonzero process exit. Do not compare only the new reserved value: a mutant implementation could accidentally leave available unchanged. Boundary tests exercise the exact allowed amount and the first rejected amount. Freezing this shallow fixture detects accidental writes; nested objects need independent coverage. A test that never fails for a broken implementation may provide false confidence.
Use equivalent contracts in a frontend test runner
import { describe, expect, it } from 'vitest';import { reserve } from './reservation.mjs';describe('reservation limits', () => { it('conserves the total count', () => { const state = { available: 3, reserved: 1 }; const next = reserve(state, 2); expect(next.available + next.reserved).toBe(4); expect(next).toEqual({ available: 1, reserved: 3 }); }); it('rejects a request beyond stock', () => { const state = { available: 0, reserved: 4 }; expect(reserve(state, 1)).toBe(state); });});Vitest and Jest provide test discovery, assertions, mocking and reporting; the examples use explicit Vitest imports, not interchangeable configuration. Use the Node environment for pure tests; use jsdom for DOM tests after installing approved compatible dependencies. The Vitest example is staged, whereas the Node example was executed locally. Measure coverage as a clue to missing branches, not as proof of correctness. Practice: add an assertion that a second request exceeding remaining stock leaves the first result unchanged.