Test Logic in Production

Production code contains testing hooks.

Meszaros, xUnit Test Patterns

Signs of Use (Symptoms)

  • if (process.env.TESTING) { ... } in production code.
  • Code paths that only exist for tests.

Why it is bad (Consequences)

  • Complexity: Production code is cluttered.
  • Risk: Testing code might accidentally run in production.

Refactoring Solution

  • Dependency Injection: Inject test doubles instead of conditionals.
  • Separate test harness from production code.

Code example

Bad

Good