Hexagonal Architecture (Ports & Adapters) in Frontend Applications vs Test Fidelity Spectrum: Unit, Component, Browser & E2E
Swapping driven adapters with in-memory fakes shifts domain tests from slow E2E runs to millisecond unit checks.
Structured Comparison Matrix
| Dimension | Hexagonal Architecture (Ports & Adapters) in Frontend Applications | Test Fidelity Spectrum: Unit, Component, Browser & E2E |
|---|---|---|
| Mental Model | Stop rewriting business rules every time an API SDK, storage engine, or UI framework churns. Hexagonal Architecture isolates pure domain logic behind typed ports so React, fetch, and IndexedDB stay swappable adapters. | Inverting the test suite into 80% E2E balloons CI past 40 minutes, while 95% mocked JSDOM suites ship broken layouts and CORS failures; match each assertion to the lowest layer that executes the real mechanism. |
| Maturity & Domain | Software Architecture | Testing & Verification |
| Prerequisites | ||
| When Not to Use | When not to use: Pure CRUD forms, static marketing pages, or thin read-only views with no client-side business invariants or multi-step workflows. Wrapping a single GET /profile call in port interfaces, mappers, and factories adds boilerplate and indirection with zero architectural payoff; use direct Server State Cache Machines with boundary validation instead. | When not to use Full-Stack E2E: Never use full-stack E2E tests to verify component styling permutations, pure business math, or exhaustive validation matrices; push those down to unit or browser-component suites where failure feedback takes milliseconds. |
Engine Mechanics & Trade-Offs
Static ES Module imports restrict domain files to pure JavaScript with zero DOM or SDK edges. At startup, a composition root binds concrete adapters into port closures. At runtime, V8 dispatches port calls through monomorphic or polymorphic inline caches while isolating domain heap objects from DOM nodes and network clients.
Key Pitfalls: Leaky infrastructure types in port signatures: Symptom: domain unit tests fail in Node.js with ReferenceError: window is not defined (or IDBDatabase is not defined), or replacing axios with fetch breaks 30 domain files. Root cause: outbound ports return SDK error classes, HTTP Response objects, browser DOM/IndexedDB ha
The test fidelity spectrum maps each verification tier to the browser and backend subsystems it actually instantiates, balancing millisecond feedback against engine realism.
Key Pitfalls: Duplicating Unit Combinatorics in E2E: Testing 40 form-validation edge cases through full-stack browser logins burns 15 minutes of CI compute to exercise a pure regex function. Mocking Everything in E2E: Stubbing auth, routing, and API serialization inside E2E tests collapses E2E fidelity down to a slow component test