Testing Provider Detection Logic

Testing Provider Detection Logic

The Lesson

When critical logic is embedded in a class that's hard to test (DOM-coupled UI class), developers sometimes copy the logic into the test file and test the copy instead. This creates a dangerous illusion of coverage: the tests pass, but they're not testing the real code. When the real code diverges from the copy, regressions are invisible.

Context

The getProviderFromExam() function was originally a method inside the QuizApp class. Since QuizApp required a browser DOM to instantiate, the test file recreated the same provider-detection logic as a standalone function and tested that copy.

What Happened

  1. Code review identified that app.test.js was testing a copy of the logic, not the real function
  2. The fix: extract getProviderFromExam() from QuizApp as a standalone exported function in app.js
  3. Update app.test.js to import and test the real export
  4. Verify all 27 existing tests still pass against the real function
  5. Add new tests for recently added providers (Anthropic, CompTIA, ISC2, etc.)

Key Insights

Applicability

This anti-pattern appears anywhere tests reimplement production logic rather than importing it: regex patterns copied into test files, calculation formulas duplicated for assertion, routing rules recreated in test helpers. The fix is always the same — extract the logic into an importable, testable unit, then test the real thing. This applies to any language or framework.

Related Lessons