Tests as the First Gate
On a site that is mostly content, the most useful tests check content, wiring and rules, not just functions. Here is the mix, and why speed matters most.
Why tests come first
Every later gate is more expensive. A failing test on a laptop costs seconds. A broken page in production costs visitors and trust. So the test suite is the first, cheapest and most important gate in the pipeline.
Speed is a feature
This site has more than 650 tests and they run in about eight seconds locally. That speed is not a luxury: a test suite that is slow does not get run. If tests take ten minutes, people push without them and let CI find the problem later. Keep the suite fast by:
- using Node's built-in runner (
node --test), with no heavy framework to load; - reading files and calling functions directly instead of starting browsers;
- starting a real server only for the few tests that need one, on a random free port.
The five kinds of test that earn their keep
1. Unit tests for logic. Pure functions with clear inputs and outputs: the function that classifies a health check result, the one that works out a seven-day median, the one that trims a page title to fit. These are quick to write and catch regressions instantly.
2. Content tests. On a writing-heavy site, most bugs are in content, not code. Tests here check that every live page has a title and a status, that every internal link points at a page that exists, that a guide has the sections its format requires, and that a draft never appears in the sitemap.
3. Wiring tests. Read the source and check that pieces are connected: a route is mounted, a page is in the menu, a new string has both an English and a Hindi version. These sound trivial and catch the most embarrassing mistakes, like a new page that nothing links to.
4. Route tests. Start the app in the test, make real HTTP requests and check the answers: private pages need a login and are never cached; a payment webhook with a bad signature is refused; a scheduled job without its secret gets a 401.
5. Guard tests. Rules that protect the system from drift: the container must not run as root, the rate limiter must not trust a header any visitor can fake, no file may build a content path by hand. Each one was written after a real mistake, so the mistake cannot come back silently.
Testing failure, not just success
The most valuable tests ask "what happens when this goes wrong?":
- the analytics service is down: does the dashboard still load?
- the database refuses a write: does the visitor get a quiet error instead of a crash?
- a content file is broken: does the page explain what is wrong instead of drawing half a page?
Real systems spend a lot of their life partly broken. Test those states on purpose.
What not to test
- Exact wording of long text, which changes often. Test that the text exists and is non-empty.
- Numbers that grow on their own (more on this in When Tests Lie).
- Third-party services themselves. Test your code's behaviour when they answer well and when they fail, using a fake.
A test is documentation
A good test name reads like a rule: "private pages are never chosen as health-check targets", "a used payment cannot be replayed after the access expires". Reading the test list is the quickest way to learn what the system promises.
Try it yourself
Pick the last bug you fixed. Write the test that would have caught it, and check that it fails on the old code and passes on the new. That one habit, a test for every fixed bug, builds a suite that matches your real risks.