跪拜 Guibai
← All articles
Frontend · JavaScript · Programmer

A Frontend Team Cut Its Unit Tests by 78% and Production Defects Didn't Budge

By ErpanOmer ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Coverage gates in CI push teams into writing tests that assert internal calls and snapshot DOM structure, and the failures become a tax on every merge in exchange for nothing. This team's six-month numbers show unit tests earn their cost only on pure logic with financial or access consequences; presentation and interaction layers are better served by runtime contract checks, type strictness, E2E smoke tests, and monitoring.

Summary

Coverage mandated at 75% in an OKR produced a test suite that looked great on a curve and did nothing else. This team's data review of 312 CI failures showed 86.9% were resolved by rewriting the test to fit the new code, 7% were flaky reruns, and only 19 caught actual bugs. Seventeen of those 19 were in price calculation, permission checks, and date handling; the 1,800 tests covering UI rendering, hooks, and page flows caught exactly zero.

The team deleted implementation-detail tests, 400+ snapshot files, and integration tests that mocked away the entire environment. What remained: edge-case-heavy unit tests for pure money and permission logic, zod schema checks that validate API response contracts at runtime, and five Playwright smoke paths for login, order, payment, refund, and listing. Six months later production defects were 41 vs 39, P0/P1 counts barely moved, and CI dropped from 14 minutes to 2:40.

The process matters as much as the cuts: a git churn script identified test files that co-change with their source 90% of the time, strict TypeScript flags were enabled to replace tests that were really checking types, and Sentry monitoring plus a 10% canary release replaced the safety net the deleted tests pretended to offer.

Takeaways
— A 9-person frontend team cut unit test cases from 2,316 to 427, dropping coverage from 78% to 31%.
— Over 12 months, 312 CI test failures occurred; 271 (86.9%) were fixed by modifying the test code, 22 were flaky, and 19 caught real bugs.
— 17 of the 19 real catches were in amount/price calculation, permission/role checks, and date/time modules; UI component, hook, and page-flow tests caught 0 real bugs.
— 400+ snapshot files existed; every failure (100%) was resolved by updating the snapshot, never by finding a defect.
— After deletion, production defects were 41 in the prior six months vs 39 in the following six months; P0/P1 counts were 1/6 vs 1/5.
— CI test time dropped from 14 minutes to 2 minutes 40 seconds.
— The team enabled TypeScript strict, noUncheckedIndexedAccess, and exactOptionalPropertyTypes, and eliminated 300+ 'any' types.
— zod contract validation for core APIs caught 6 backend field changes in test environments and 2 in production over six months.
— Playwright smoke tests cover only 5 critical paths: login, order, payment sandbox, refund, product listing, and run in under 3 minutes.
— A git churn script flags test files that co-change with their source >90% of the time as likely implementation-detail tests.
— Production monitoring added: Sentry release health, API error-rate alerts, white-screen detection, and a 10% canary for 30 minutes before full rollout.
Conclusions

Coverage as an OKR target creates tests written to move a number, then maintained to keep that number moving; the 86.9% test-modification rate is the evidence of that incentive.

A test suite where every failure leads to updating the test rather than the product code trains the team to ignore red lights, which is worse than no tests at all.

The zero real bugs caught by UI and hook tests is a strong signal that React component behavior is better guarded by the type system and E2E checks than by unit tests that mock the environment.

Replacing mock-heavy integration tests with runtime contract validation in zod catches backend field changes, a class of bugs that mocked tests structurally cannot see.

Dropping from 78% to 31% coverage without a defect increase suggests coverage percentage alone is a poor proxy for test value; value comes from the expected cost of a bug in that module.

Concepts & terms
Coverage gate
A CI rule that blocks a merge request if the unit test coverage metric falls below a configured threshold, commonly set as a percentage.
Snapshot test
A test using toMatchSnapshot() that serializes a component's rendered output and compares it to a stored snapshot; any change in output fails until the snapshot is updated with vitest -u.
Test churn
A measure of how often a test file changes in the same commit as its corresponding source file; a high churn ratio indicates the test is bound to implementation details rather than user behavior.
Runtime contract validation
Checking an API response against a schema (here with zod) at runtime, so a mismatch between frontend assumptions and backend field types fails fast in dev/test and is reported in production.
From the discussion
Featured comments
去吧波波鸟

Every snapshot failure just gets a direct -u update; that line had me shaking my head. Once coverage gets written into OKRs, tests turn from a defense line into a report.

ErpanOmer

[watching]

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗