- Pick the right evidence for each change
- Write an E2E that fails for the right reason
- Audit what a test cannot assert
- Read metrics without fooling yourself with one run
The evidence ladder
Each step answers a different question. The unit test proves a rule; the E2E proves a person can complete the task; the headless audit proves DOM invariants; field metrics prove how it feels in production.
The common mistake is jumping to the last step: looking at a Lighthouse number and believing it proves the feature works.
- Unit: rules and transformations.
- E2E: browser journeys and accessibility.
- Headless: DOM state, console, transitions.
- Field: what happens on real devices.
Playwright with axe
An E2E should assert behaviour, not implementation. “The button exists” is weak; “the language switch keeps the route” is not.
Axe joins as one more assertion: a serious violation fails the test like any `expect`.
test("switching from an English article keeps the route", async ({ page }) => {
await page.goto("/en/blog/grafos");
await expect(page.locator("html")).toHaveAttribute("lang", "en");
await page.getByRole("button", { name: /^ES/ }).first().click();
await expect(page).toHaveURL(/\/blog\/grafos$/);
await expect(page.locator("html")).toHaveAttribute("lang", "es");
});
test("axe reports nothing serious on the home", async ({ page }) => {
await page.goto("/");
const results = await new AxeBuilder({ page })
.withTags(["wcag2a", "wcag2aa"])
.analyze();
const blocking = results.violations.filter(
(violation) => violation.impact === "serious" || violation.impact === "critical",
);
expect(blocking.map((violation) => violation.id)).toEqual([]);
});Auditing what a test cannot see
Some invariants are not clicks: no console errors, a transition that settles on `idle`, a pointer that publishes CSS variables, a card that opens exactly one view transition.
For those, a script driving the browser over the protocol and asserting on the DOM beats a person staring at the screen.
await evaluate(`(() => {
const card = document.querySelector("[data-spotlight]");
const rect = card.getBoundingClientRect();
card.dispatchEvent(new PointerEvent("pointermove", {
bubbles: true,
clientX: rect.left + rect.width / 2,
clientY: rect.top + rect.height / 2,
}));
})()`);
const state = await evaluate(`(() => {
const card = document.querySelector("[data-spotlight]");
return {
active: card.style.getPropertyValue("--spot-active"),
x: card.style.getPropertyValue("--spot-x"),
};
})()`);
expect("spotlight follows the pointer", state.active === "1" && state.x !== "");The number trap
A single Lighthouse run is not a measurement, it is an anecdote. On mobile, TBT swings hundreds of milliseconds between runs of the same build. Run it three times and look at the median.
And the number can improve for the wrong reason. Animating the opacity of the LCP element disqualifies it as a candidate: LCP “gets worse” even though the page looks the same. Read the metric with its breakdown, not with the score.
# three runs, same build, same machine
for i in 1 2 3; do
npx lighthouse https://example.com --only-categories=performance \
--output=json --output-path="lh-$i.json" --chrome-flags="--headless=new"
done
# the median, not the best number
node -e "const r=[1,2,3].map(i=>require('./lh-'+i+'.json'));
console.log(r.map(x=>Math.round(x.categories.performance.score*100)).sort())"The server fetches data and serializes it as JSON.
A test that fails for the right reason
Write an E2E that fails if a route loses its canonical. Then break the canonical on purpose and verify the test fails with a message that explains the problem, not with a timeout.
Show a hint
Assert on what is observable (attribute, text, URL), never on internal classes or implementation.