Testing JavaScript
Protect behavior with focused unit, integration, and user-facing tests.
Tests turn important expectations into executable examples. The best test level is the smallest one that proves behavior with confidence.
A focused unit test
progress.test.js
import test from "node:test";
import assert from "node:assert/strict";
import { progressPercent } from "./progress.js";
test("calculates rounded progress", () => {
assert.equal(progressPercent(3, 8), 38);
});
test("handles an empty course", () => {
assert.equal(progressPercent(0, 0), 0);
});
Use unit tests for calculation and parsing, integration tests for modules working together, and browser tests for critical user journeys.
Test outcomes, not implementation
- Give every test one observable reason to fail.
- Cover normal, boundary, failure, and accessibility behavior.
- Keep fixtures small and readable.
- Avoid asserting private function calls or internal markup details.
- Reproduce a bug with a failing test before fixing it.
- Run deterministic tests in continuous integration.
Keep this
Test public behavior, choose the cheapest trustworthy level, and make failures explain the broken expectation.
学习位置已保存在此设备上。