What I test, and what I deliberately do not
Conversations about testing usually start with the wrong question: "what is our coverage?".
It is a bad question, because the answer means nothing. I have seen suites with one hundred percent coverage that catch no real bugs at all. They prove exactly one thing: the code does what it was written to do. Which you already knew.
The right question is different: which code, if it breaks, must I find out about?
Three kinds of code
In any project the code splits into three groups, and each deserves different treatment.
Group one: rules. Price calculation, discount application, attendance percentages, permission checks. This code is pure: data in, result out. It is easy to test and expensive when it breaks.
This is the most useful place for tests. Most of my effort goes here.
Group two: connections. Database queries, HTTP handlers, external API calls. There is little logic here but plenty of mistakes — a wrong field name, a forgotten await, the wrong status code.
Covering this group with unit tests is usually pointless: you put a mock where the database was and end up testing your own mock. What you need here are integration tests — against a real database.
Group three: glue. Reading config, assembling objects, simple pass-throughs. If this breaks, the build or the very first request will tell you.
I deliberately do not test this group. Tests for it cost time, catch nothing, and are the first thing in your way during a refactor.
When mocks do damage
The mock is the most abused tool in testing.
The reason is understandable: it makes tests fast and easy. The problem is that when you write a mock, you are writing down your assumption about how the system behaves. If the assumption is wrong, the test is green and production falls over.
A practical example. You mock a database call so it returns an array. The test passes. In production the ORM returns null on an empty result, and the code dies right there. The test could never have caught it, because it was checking your mental model rather than reality.
My rule is this: I do not mock my own code. Mocks only go on boundaries I do not control — a payment provider, SMS delivery, a third-party API.
Instead of mocking the database I use a real one. A container comes up in Docker, migrations run, tests run, the container goes away. It is slower — but the result reflects reality.
What a test should check
The most common mistake is tying a test to the implementation.
A bad test:
it('calls applyRule inside calculateDiscount', () => {
const spy = vi.spyOn(rules, 'applyRule')
calculateDiscount(order)
expect(spy).toHaveBeenCalled()
})This freezes the internal structure of the code. Tomorrow you rename applyRule or move the logic inward — and the test fails even though the behaviour did not change.
A good test:
it('applies a 10% discount to orders over 5 items', () => {
expect(calculateDiscount({ items: 6, total: 100000 })).toBe(90000)
})This checks the outcome. Rewrite the internals as much as you like — the test supports you rather than blocking you.
A simple check: if a refactor forces you to change the test as well, the test was written at the wrong level.
Edge cases are where the value is
The real value of a test suite is not in the "everything works" path. You will exercise that by hand anyway.
The value is at the edges:
- The empty list. What is the average of zero items?
- The boundary number. If the discount is "more than 5", what happens at exactly 5?
- Missing data. Both
nullandundefined. - The repeated call. Send the same request twice — do you get two orders?
- Time boundaries. The last day of the month, the year rollover, the end of a term.
That last one matters especially in the school system: term and academic-year boundaries touch almost every report, and they misbehave precisely on the boundary days.
Sometimes not writing tests is right
This part is awkward to say, but honesty is worth more: not every project needs tests.
When I built the comment system for my own blog, I deliberately wrote no unit tests. The reasons were clear: small amount of code, almost no logic, low cost of failure (if a comment fails to render nobody is harmed), and I write it once and leave it.
I spent that time elsewhere — manually verifying database permissions against specific scenarios. Because there the cost of a mistake was genuinely high.
The decision criterion is this: cost of failure × likelihood of recurrence. When both are low, not writing tests is a sound engineering decision, not laziness.
The reverse also holds: the window-management script I wrote for KWin has 22 tests. The logic there is intricate, edge cases are plentiful, and every change carries real regression risk. Same person, same month — completely different call.
A practical starting point
If your project has no tests at all, do not try to cover everything. Start like this:
- Find the calculation function most tied to money or trust. Write tests for it, including edge cases.
- Take the last bug that reached production and write a test that reproduces it. That test pays for itself immediately.
- Write integration tests, against a real database, for your three most important HTTP flows.
- Stop, and live with that for a month.
Then look back: which tests actually saved you, and which only got in the way of refactoring. Delete the second group.
A test suite is code, and all code is a cost. Treat it as an investment: do not put money where there is no return.