A test suite can hit 100 percent coverage and still miss the bug that takes your app down on a Friday night. Coverage counts which lines ran. It says nothing about whether any test checked the right thing. To write tests that catch bugs, you have to test behavior, not lines: feed the code realistic inputs, check the outputs that matter, and aim at the places where the code is most likely to be wrong.
This guide shows how to do that in practice: pick inputs that expose mistakes, write assertions that check real behavior, test the edges where bugs hide, and use failures from production as the raw material for tests that stop the same bug from coming back.
How to Write Tests That Catch Bugs: Start from Behavior, Not Coverage
Coverage tools measure which statements, branches, and functions ran while tests executed. That number is easy to game. A test can call a function with one input, assert nothing, and still add to the coverage total. The line ran, the test passed, and the function can still be wrong for every other input. This coverage vs behavior testing gap is the reason a green suite with high coverage still ships bugs.
Consider a discount function with a bug in one branch:
function discount(price, isMember) {
if (isMember && price > 100) {
return price * 0.8;
}
return price;
}
A single test with a member and a price of 150 covers both branches and pushes this function to 100 percent coverage. But it never checks what happens for a non-member, a price of exactly 100, or a price of 101. The coverage number looks perfect, and three of the four cases are untested. Behavior is what you need to check, and coverage is only a map of where you have not looked yet.
Use coverage as a flashlight, not a score. Open the coverage report, find the branches your tests never touched, and ask whether each one matters. If it does, write a test for it. If it does not, delete the dead branch instead of writing a test to protect it.
Start from the Bug, Not from the Function
The best tests come from asking one question before you write any code: what could go wrong here? List the failure modes first, then write a test for each one. A login function can fail on a wrong password, an expired token, a locked account, or a missing user. A checkout function can fail on an empty cart, an out-of-stock item, or a declined card. Each failure mode is a test waiting to be written.
This habit also changes how you read code during review. Instead of checking whether the code looks right, you check whether the tests cover the ways it could be wrong. A pull request with tests for the happy path only is a pull request that has not been tested yet, no matter what the coverage number says.
Choose Inputs That Expose Mistakes
Most bugs live in the gap between the input the developer imagined and the input the user actually sends. Picking good test inputs means deliberately crossing that gap.
Test the boundaries first. If a function accepts prices above 100, test 99, 100, and 101. If a list holds at most 10 items, test 0, 1, 9, 10, and 11. Off-by-one errors are the most common bugs in code, and boundary values are the cheapest way to catch them. A test with a single round number like 100 will never find an off-by-one error, because the bug and the test agree by accident.
Test empty and missing values next. Empty strings, empty arrays, null, and undefined break more code than any exotic input. A function that works for a full list and crashes on an empty one is a function that was never tested with real data, because real data is often empty at the start.
Then test the messy middle: long strings, unicode characters, negative numbers, and amounts with decimals. You do not need a test for every weird value. You need a few tests that prove the code handles the kinds of input that actually show up in production logs. If you work on an API, the best API testing tools for 2026 roundup covers how to send those awkward inputs to a live endpoint and see what comes back.
// discount.test.js
const { discount } = require("./discount");
test("member with price above 100 gets 20 percent off", () => {
expect(discount(150, true)).toBe(120);
});
test("price of exactly 100 gets no discount", () => {
expect(discount(100, true)).toBe(100);
});
test("non-member gets no discount at any price", () => {
expect(discount(150, false)).toBe(150);
});
test("zero price returns zero", () => {
expect(discount(0, true)).toBe(0);
});
Write Assertions That Check Real Behavior
An assertion is only as good as what it checks. Weak assertions are the quiet reason test suites miss bugs, and they are easy to write by accident.
The weakest form is asserting that something is defined. A test that checks expect(result).toBeDefined() passes for almost any return value, including a wrong one. It proves the function ran without crashing, and nothing else. One of the test assertions best practices that matters most is to replace it with an assertion on the actual value, or on the exact field that matters.
The next weak form is asserting on the wrong thing. A test that checks the length of a list but not its contents will miss a sorting bug, a duplication bug, and a filtering bug all at once. Check the contents, the order, and the specific items you expect. The more specific the assertion, the more bugs it can catch, and the clearer the failure message when it does.
Strong assertions also check side effects. If a function should send an email, write to a log, or update a record, assert that the side effect happened, not just that the function returned. A checkout test that checks the return value but not the charge will miss the bug that charges the customer twice.
Test Edge Cases: The Places Where Bugs Hide
Bugs cluster in a few predictable places. Once you know where to look, you can aim your tests there instead of spreading them evenly across the code.
Async code is the first cluster. Race conditions, unhandled rejections, and promises that resolve in the wrong order cause failures that pass on a fast machine and fail on a slow one. Test async functions with real waits, not with fixed timeouts, and check what happens when a promise rejects. Understanding how the JavaScript event loop actually works helps here, because many async bugs come from assuming code runs in the order it was written.
Date and time handling is the second cluster. Time zones, daylight saving changes, and month boundaries break code that looks fine in a single-time-zone test. Test with dates near midnight, at the end of February, and across a time zone change if your users live in more than one.
Error handling is the third cluster, and the most neglected. Code that handles the happy path well and the error path badly is the norm, not the exception. Write a test for every error branch: the failed request, the invalid input, the missing file. If the error path has no test, assume it is broken until proven otherwise.
Use Production Failures as Test Material
Every bug that reaches production is a test you did not write yet. The fix is a two-step habit: reproduce the bug as a failing test, then fix the code until the test passes. The failing test goes in first, before the fix, so you know the test actually catches the bug and is not just passing by accident.
Keep a short list of the bugs that hurt most: the ones that lost data, charged the wrong amount, or broke login for real users. Those are the tests your suite must never lose. When you refactor the code around them, those tests are the net that catches a regression before it ships.
Refactor Tests the Way You Refactor Code
Tests are code, and they rot the same way code rots. A test that nobody can read will not be trusted when it fails, and a test that takes a minute to run will be skipped. Keep the suite fast, clear, and honest. State that persists between calls can also leak between tests, and JavaScript closures explained with real code examples shows how that scope works.
Write tests that catch bugs, and the coverage number takes care of itself. Start from the failure modes, pick inputs that separate the branches, assert on the values that matter, and turn every production bug into a test before you fix it. A suite built that way does not just measure your code. It protects it.