Pick a runner (Vitest, Jest, node:test), then for each behaviour write a small test that follows Arrange → Act → Assert: set up inputs, call the code, check the output or observable effect. Test behaviour through the public interface, not internals; cover the happy path, edge cases and error cases; keep tests isolated, deterministic and fast; and replace only true external dependencies (network, time, randomness) with test doubles.
// cart.js
export function cartTotal(items, { discountPercent = 0 } = {}) {
if (!Array.isArray(items)) throw new TypeError('items must be an array');
const subtotal = items.reduce((sum, { price, qty }) => sum + price * qty, 0);
const discounted = subtotal * (1 - Math.min(discountPercent, 100) / 100);
return Math.round(discounted * 100) / 100;
}
// cart.test.js
import { describe, it, expect } from 'vitest';
import { cartTotal } from './cart.js';
describe('cartTotal', () => {
it('sums price × quantity', () => {
// Arrange
const items = [{ price: 10, qty: 2 }, { price: 5.5, qty: 1 }];
// Act
const total = cartTotal(items);
// Assert
expect(total).toBe(25.5);
});
it('returns 0 for an empty cart', () => {
expect(cartTotal([])).toBe(0);
});
it('applies a percentage discount', () => {
expect(cartTotal([{ price: 200, qty: 1 }], { discountPercent: 15 })).toBe(170);
});
it('never goes below zero for discounts over 100%', () => {
expect(cartTotal([{ price: 50, qty: 1 }], { discountPercent: 150 })).toBe(0);
});
it('rounds to cents', () => {
expect(cartTotal([{ price: 0.1, qty: 3 }])).toBe(0.3);
});
it('rejects non-array input', () => {
expect(() => cartTotal(null)).toThrow(TypeError);
});
});
Table-driven tests keep many similar cases readable:
it.each([
[[], 0],
[[{ price: 1, qty: 1 }], 1],
[[{ price: 2.5, qty: 4 }], 10],
])('cartTotal(%j) = %d', (items, expected) => {
expect(cartTotal(items)).toBe(expected);
});
null/undefined; unusual strings (Unicode, whitespace).if path, error paths, early returns.Design for testability: pass dependencies in rather than importing singletons.
export function createGreeter({ now = () => new Date() } = {}) {
return (name) => (now().getHours() < 12 ? `Good morning, ${name}` : `Hello, ${name}`);
}
it('greets in the morning', () => {
const greet = createGreeter({ now: () => new Date('2026-01-01T09:00:00') });
expect(greet('Ada')).toBe('Good morning, Ada');
});
When injection isn't possible, use the runner's tools: vi.fn()/jest.fn() spies, module mocks (vi.mock), fake timers (vi.useFakeTimers(), vi.setSystemTime()), and MSW for HTTP.
beforeEach/afterEach (or enable restoreMocks in config).npx vitest # watch mode
npx vitest run --coverage
Run tests in CI on every pull request and treat flaky tests as bugs.
Spotted a mistake or a rights concern? Report an issue