Skip to main content

Mock libraries are imported only in test files

ab-000299 · ai-slop-test-theater.mock-hygiene.mock-imports-bounded-to-tests
Severity: mediumactive

Why it matters

Mock libraries like msw, nock, and sinon are designed to intercept network calls and fabricate responses. When they leak into production source files, real API requests get hijacked by test doubles — users see stubbed data, webhooks never reach their destinations, and payment flows silently return mock success responses. This also bloats production bundles with test-only code and can mask outages because the mock always returns 200 OK regardless of backend health.

Severity rationale

Medium because the breakage ranges from silent data corruption to full feature outage depending on which request the mock intercepts.

Remediation

Move each flagged mock import into the corresponding test file under tests/ or __tests__/, or into a test-only setup module. If the production file genuinely needs a stubbed response (feature flag, offline mode), use a real implementation behind an environment check instead of a mock library.

// tests/setup.ts — test-only, never imported from src/
import { setupServer } from 'msw/node'
export const server = setupServer()

Detection

  • ID: ai-slop-test-theater.mock-hygiene.mock-imports-bounded-to-tests

  • Severity: medium

  • What to look for: Walk all source files (excluding test files). Count all imports of mock libraries: msw, @mswjs/data, nock, sinon, testdouble, mock-fs, aws-sdk-mock, fetch-mock, jest-fetch-mock. Mock libraries should never be imported from production source files — that's a sign that test code leaked into production.

  • Pass criteria: 0 mock library imports in non-test source files. Report: "X non-test source files inspected, 0 mock library imports."

  • Fail criteria: At least 1 non-test source file imports a mock library.

  • Skip (N/A) when: No mock libraries are in package.json dependencies.

  • Detail on fail: "1 mock library imported in production: src/lib/api-client.ts imports 'msw' — test mocks shouldn't be in production code paths"

  • Remediation: Mock libraries are for tests only. Their imports in production code mean either the file should be a test file OR a test-only import leaked into a production module:

    # Find the import
    grep -rn "from 'msw'" src/
    
    # Move it into the corresponding test file, or create a test-only setup:
    # tests/setup.ts
    import { setupServer } from 'msw/node'
    export const server = setupServer()
    

Taxons

History