Reference article · Programming
Debugging and tests: finding and checking errors
Debugging investigates unexpected behaviour. Automated tests check specific expectations. Together they help fix a failure and detect a recurrence after later changes.
Reproduce the problem first
Record steps, inputs, expected output and actual output. Reduce the example while keeping the failure. For a wrongly initialized counter, examine the isolated calculation before investigating the whole interface.
Console and breakpoints
The console displays messages and errors; a stack trace shows the execution path. A DevTools breakpoint pauses execution so you can inspect variables. Step through instructions to find where the value becomes wrong. Excessive logging can obscure useful evidence.
A minimal Node.js test
Use a Node.js version with the built-in test runner. Save this as add.test.mjs; .mjs enables ES modules. Run node --test add.test.mjs in that folder.
import test from "node:test";
import assert from "node:assert/strict";
function add(a, b) {
return a + b;
}
test("adds two numbers", () => {
assert.equal(add(4, 7), 11);
});
test("zero preserves the value", () => {
assert.equal(add(4, 0), 4);
});Coverage and limits
These tests check two numerical outputs, not a UI, API or every possible input. The function is inline to keep this example self-contained; in a project, import the real function so tests detect implementation changes.
Choose useful cases
Include a typical case and relevant limits such as zero or an empty collection. Define invalid-input behaviour first. Check expected behaviour, rather than only whether execution throws.
Regression exercise
- Temporarily replace addition with subtraction and verify failure.
- Restore addition and verify success.
- Add a negative-number case and calculate its expectation manually.
References
Related articles
- Fundamentals: variables, conditions and loops
- Semantic HTML and page structure
- CSS: box model, Flexbox and Grid
- JavaScript: functions, parameters and scope
- JavaScript: arrays, objects and data transformations
- DOM and events: connect JavaScript to HTML
- Promises, async/await and fetch requests
- HTTP and web APIs: requests, responses and status codes
- JSON: format, parsing and data validation
- Git: changes, commits, branches and remotes
- Node.js and npm: project scripts and dependencies
- Colour formats: HEX, RGB and HSL
- UUID identifiers
- Random values in JavaScript
