Architecture Fitness Functions

Tests that protect architectural decisions, not just behaviour.

0/2 done

Theory

A fitness function is an objective, automated check that an architectural property still holds. Examples:

  • domain package must not import from infrastructure (ArchUnit, Dependency-Cruiser).
  • P99 latency stays below 200ms (load test).
  • No cyclic dependencies between modules.

Without them, architecture decays the moment the architect stops looking.

Worked example — a complete dependency-cruiser rule

Worked example — anatomy of a dependency-cruiser rule.

dependency-cruiser (or eslint-plugin-import / ArchUnit in Java) reads your .dependency-cruiser.js config and fails the build when an import crosses a forbidden edge. Every rule has the same four fields:

FieldPurpose
nameHuman-readable id printed in CI when the rule trips.
severity'error' fails the build; 'warn' is informational.
fromPath pattern (regex) — which files this rule applies to.
toPath pattern (regex) — which imports are forbidden from those files.

A complete, runnable config:

module.exports = {
  forbidden: [
    {
      name: 'domain-must-not-import-infrastructure',
      severity: 'error',
      from: { path: '^src/domain' },
      to:   { path: '^src/infrastructure' }
    }
  ]
};

The rule above is the executable form of the hexagonal contract from Level 4: if any file under src/domain/ ever imports a module under src/infrastructure/, the build dies. That's a fitness function — architecture protected by CI, not by code review.

Reading in progress · 0 of 2 activities done