Skip to content

Dead code detector should flag code used only in tests #35

@fixcik

Description

@fixcik

Problem

Currently, the dead code detector does NOT flag functions and variables as dead code when they are only referenced in test files. However, code that exists only to support testing and is never used in production code could be considered dead code (or at least should be configurable to be treated as such).

Current Behavior

  • A function/variable defined in source code
  • Only used/imported in test files (e.g., *.test.ts, *.spec.ts, __tests__/*)
  • NOT used anywhere in production code
  • Does NOT get flagged as dead code

Expected Behavior

Code that is used ONLY in tests and never in production should be flagged as dead code, or there should be a configuration option to control this behavior.

Rationale

  1. Test-only code might indicate design issues - If a function is only used in tests, it might mean the abstraction is unnecessary
  2. Maintenance burden - Test-only functions still need to be maintained even though they provide no production value
  3. Code bloat - Increases bundle size for no production benefit
  4. Configurability - Different teams have different policies about test-only code

Possible Solutions

  1. Add configuration option - flag_test_only_code: bool to control whether test-only usage should be considered dead
  2. Separate category - Flag as test_only_code instead of dead_code for clarity
  3. Exclude test helpers - Allow patterns to exclude legitimate test utilities (e.g., src/test-utils/*)

Example

// src/utils.ts
export function testHelper() { 
  // Only used in tests, never in production
}

// src/utils.test.ts
import { testHelper } from './utils';

test('something', () => {
  testHelper();
});

Currently testHelper is NOT flagged, but arguably it should be (with proper configuration).

Related

This relates to the detect_test_files functionality and could use similar test file pattern detection.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions