liveBook · Manning

Extract adapter pattern

As explained in The Art of Unit Testing 3e

A testability pattern that isolates an inherently asynchronous operation behind an abstraction so it can be replaced by synchronous behavior during testing.

The extract adapter pattern makes asynchronous code easier to unit-test by isolating the asynchronous dependency behind a smaller adapter. Tests can replace that adapter with controlled, synchronous behavior instead of contacting the real dependency. section 6.2 section 6.2.2

Unlike extracting pure logic into test entry points, this pattern extracts the dependency itself. The adapter can also expose a simplified interface using the consumer’s terminology, hiding implementation complexity and limiting dependency changes to one module. section 6.2 section 6.2.2

Testing a website verifier

A network adapter hides fetching and returns { ok, text }. The website verifier consumes that interface, while a test supplies a fake returning illustrative content or unexpected content. The verifier can then test its success and failure decisions without relying on the network.

Figure 6.7 Wrapping the node-fetch module with our own network-adapter module helps us expose only the functionality our application needs, expressed in the language most suitable for the problem at hand.
06-07

The figure shows how wrapping node-fetch in a project-specific network adapter exposes only the functionality the application needs. section 6.2.2

How The Art of Unit Testing 3e covers it

How deep it goes
Demonstrates adapter extraction, interface simplification, mocking, functional injection, and constructor injection.
Examples
Uses a node-fetch network adapter and website verifier tested with Jest mocks, functional fakes, and typed constructor-injected substitutes.
What it assumes
Assumes dependencies and unit testing; teaches adapter abstraction, simplified interfaces, and injection through JavaScript and typed examples.
What you can do after
Apply it to unit-test asynchronous consumers with controlled adapter behavior and reduced reliance on real network operations.
How it connects
Connects asynchronous unit testing with dependency replacement, interface segregation, mocking, and dependency injection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Unit Testing 3e. The section links point to the parts of the book it draws on.