Which tests should you write for an existing project?
When a project has few tests, starting with behaviour that would be costly to break helps choose the first scenario to protect.
Updated on
Choose the behaviour to protect
When a project has few tests, trying to cover all the code at once can scatter effort. I suggest starting with a question: which behaviour would be costly to break in the next change? If a form loses input after a network error, there is already a precise case to protect.
Coverage indicates which lines ran during tests. By itself, it does not say whether assertions protect the right result. A suite can pass through a function without checking the error that actually bothers users. Expected behaviour should therefore be stated before choosing the file to test.
Choose a boundary that remains useful
- A pure rule: check its inputs, outputs and business edge cases.
- A form: check input, errors and the visible result after submission.
- An exchange between services: check the contract, error responses and transmitted data.
- A critical journey: check that essential steps work together.
These levels complement one another. A business-rule test can explain precisely why a calculation is wrong. A journey test can reveal that the button no longer calls that calculation. The right choice depends on the failure you want to detect and the cost of preparing the scenario. Reusing repository tools avoids adding infrastructure for a single case.
Make the scenario explicit
A readable scenario specifies the initial situation, action and expected result. Its data should explain the case without depending on a real service or implicit execution order. When time is involved, a fixed date in the test makes verification reproducible.
import assert from "node:assert/strict";
import { test } from "node:test";
test("une page suivante conserve la catégorie", () => {
const params = new URLSearchParams({
categorie: "conseils",
page: "2",
});
assert.equal(params.get("categorie"), "conseils");
assert.equal(params.get("page"), "2");
});This example illustrates the form of an assertion. In a project, the test should call the function that actually builds the URL or click the rendered link. Testing only data manufactured inside the test, as here, does not protect the application. The connection to real behaviour is what gives the check its value.
Check that the test can fail
Before concluding, temporarily changing the targeted behaviour checks whether the test detects it. If the category disappears from a link and the suite remains green, protection is insufficient. Restore the code afterwards and rerun the check. This simple control avoids confusing a suite that runs with a suite that protects.