# Test files present and organized

- **Pattern:** `ab-002142` (`code-quality-essentials.testing.test-files-present`)
- **Severity:** medium
- **Lifecycle:** active
- **Last modified:** 2026-04-18
- **Canonical URL:** https://auditbuffet.com/patterns/ab-002142
- **License:** CC-BY-4.0 — attribute to AuditBuffet Pattern Catalog (https://auditbuffet.com/patterns/ab-002142)

## Why it matters

A codebase with no test files has no automated verification that the core logic works correctly after a change. Every refactor, dependency upgrade, or new feature becomes a manual regression risk. ISO 25010:2011 §6.5.5 identifies absence of test infrastructure as a reliability defect. For AI-built projects specifically, the lack of tests is a compounding risk: the original author may not remember the edge cases, and future changes can silently break behavior that was never encoded as an assertion.

## Severity rationale

Medium because absent tests mean every change ships with no automated regression check, making regressions in business logic invisible until a user reports them.

## Remediation

Set up a test framework appropriate for your stack. For Next.js/React with TypeScript, Vitest + Testing Library is the standard starting point.

```bash
npm install -D vitest @testing-library/react @testing-library/user-event jsdom
```

Add to `package.json`:
```json
{
  "scripts": {
    "test": "vitest",
    "test:coverage": "vitest run --coverage"
  }
}
```

Create your first test co-located with the module it covers:
```typescript
// src/lib/format-price.test.ts
import { describe, it, expect } from 'vitest'
import { formatPrice } from './format-price'

describe('formatPrice', () => {
  it('formats a whole dollar amount', () => {
    expect(formatPrice(1000)).toBe('$10.00')
  })
  it('returns "Free" for zero', () => {
    expect(formatPrice(0)).toBe('Free')
  })
})
```

## Detection

- **ID:** `test-files-present`
- **Severity:** `medium`
- **What to look for:** Search the project for test files using these patterns: `**/*.test.ts`, `**/*.test.tsx`, `**/*.spec.ts`, `**/*.spec.tsx`, `**/__tests__/**`. Estimate the ratio of test files to source files — aim for at least 50% of business logic modules having a corresponding test. Check whether tests are co-located with source files (e.g., `utils/parse.ts` and `utils/parse.test.ts` side-by-side) or collected in a `__tests__` directory. Examine `package.json` for a `test` script — its absence suggests testing is not part of the development workflow. Also check whether a test framework (jest, vitest, mocha) is present in `devDependencies`.
- **Pass criteria:** Test files are present covering at least 50% of business logic modules. A `test` script is defined in `package.json`. A test framework is in `devDependencies`.
- **Fail criteria:** No test files found anywhere in the project, or only placeholder/skeleton test files with no actual assertions.
- **Skip (N/A) when:** Project is purely a static asset collection with no custom code logic (no JavaScript/TypeScript modules).
- **Detail on fail:** `"No test files (*.test.ts, *.spec.ts, __tests__/) found; testing framework not configured in package.json"`
- **Remediation:** Set up a test framework appropriate for your stack. For Next.js/React with TypeScript, Vitest or Jest with Testing Library is standard:

  ```bash
  npm install -D vitest @testing-library/react @testing-library/user-event jsdom
  ```

  Add to `package.json`:
  ```json
  {
    "scripts": {
      "test": "vitest",
      "test:coverage": "vitest run --coverage"
    }
  }
  ```

  Create your first test alongside the module it covers:
  ```typescript
  // src/lib/format-price.test.ts
  import { describe, it, expect } from 'vitest'
  import { formatPrice } from './format-price'

  describe('formatPrice', () => {
    it('formats a whole dollar amount', () => {
      expect(formatPrice(1000)).toBe('$10.00')
    })
    it('returns "Free" for zero', () => {
      expect(formatPrice(0)).toBe('Free')
    })
  })
  ```

## External references

- iso-25010 maintainability.testability

Taxons: code-quality

HTML version: https://auditbuffet.com/patterns/ab-002142
