nullables
npx skills add https://github.com/lexler/skill-factory --skill nullables
Agent 安装分布
Skill 文档
Nullables: Testing Without Mocks
STARTER_CHARACTER = âï¸
The Problem
External I/O is slow and flaky. Tests hitting real databases, APIs, or file systems run slow and fail randomly. We want tests that run in milliseconds and never fail due to network issues.
Mocking libraries solve speed but introduce a new problem: they couple tests to implementation by verifying specific method calls. Test code using mocking libraries is brittleâit breaks when code is refactored, even when behavior is unchanged.
The Solution
Nullables are production code with an “off switch” for infrastructureânot test doubles, but real code you can ship (dry-run modes, cache warming, offline operation). They enable narrow, sociable, state-based tests:
- Narrow: Each test focuses on one class/module, not broad end-to-end flows
- Sociable: Tests use real dependenciesâonly infrastructure I/O is neutralized. (Contrast with “solitary” tests that mock everything, isolating the class under test.)
- State-based: Assert on outputs and state, not on which methods were called
When to Use
Use Nullables for:
- Code that talks to external systems (HTTP, files, databases, clocks, random)
- Third-party libraries you don’t control
- Non-deterministic operations
Don’t use Nullables for:
- Pure logic â test directly, no wrapper needed
- Your own classes â make them Nullable directly, or null their dependencies
Greenfield: Add wrappers incrementally as tests demandâdon’t over-engineer upfront.
Existing codebase: See migration.md for incremental conversion strategies.
The Foundation: A-Frame Architecture
A-Frame is the architectural insight that makes Nullables work especially well. Traditional layered architecture stacks Logic on top of Infrastructure, making Logic depend on slow, brittle I/O. A-Frame makes them peers instead:
Application (coordinates)
â â
Logic (pure, tested) Infrastructure (Nullables)
Key rule: Logic never imports Infrastructure directly. Application coordinates between them via Logic Sandwich: read â process â write.
- Logic â pure functions, no I/O
- Infrastructure â wrapped with
create()/createNull() - Application â thin coordination layer
This separation lets you swap real infrastructure for nulled versions without touching Logic. For full details, see a-frame.md. For event-driven code, see event-driven.md.
Core Pattern: Two Factory Methods
Every infrastructure wrapper has two creation paths:
class Clock {
static create() {
return new Clock(Date); // Real system clock
}
static createNull(now = "2020-01-01T00:00:00Z") {
return new Clock(new StubbedDate(now)); // Controlled clock
}
constructor(dateClass) {
this._dateClass = dateClass;
}
now() {
return new this._dateClass().toISOString();
}
}
// Embedded stub - lives in production code, not test files
class StubbedDate {
constructor(isoString) {
this._time = new Date(isoString).getTime();
}
toISOString() {
return new Date(this._time).toISOString();
}
}
Key principles:
createNull()parameters match the caller’s abstraction level (ISO strings, not milliseconds)- Embedded stubs live alongside the wrapper, implementing only what’s actually used
- Add Output Tracking to observe what was written
For complete construction details, see infrastructure-wrappers.md.
Testing with Nullables
Every wrapper follows the same pattern. Here’s how you test code that uses one:
describe("App", () => {
it("transforms input and writes result", () => {
const { output } = run({ args: ["hello"] });
assert.deepEqual(output.data, ["uryyb\n"]); // ROT-13
});
function run({ args = [] } = {}) {
const commandLine = CommandLine.createNull({ args });
const output = commandLine.trackOutput();
new App(commandLine).run();
return { output };
}
});
Tests exercise real App code. Only infrastructure I/O is neutralized. The run() helper protects tests from constructor changes (Signature Shielding).
Testing Philosophy
- State-based, not interaction-based â verify what was produced, not which methods were called
- Sociable, not solitary â tests use real dependencies; only infrastructure is nulled. Bugs cause multiple test failures, pinpointing the problem
- Paranoic Telemetry â assume everything fails. Test error paths, timeouts, and failures as thoroughly as happy paths
- Collaborator-Based Isolation â use dependencies’ own methods in assertions rather than hardcoding expectations:
// BAD: Breaks if format changes (also leaks implementation details into your clients, creates bad coupling) assert.deepEqual(output.data, [{ level: "info", message: "Done", ts: 123 }]); // GOOD: Uses dependency's format assert.deepEqual(output.data, [logger.formatEntry("info", "Done")]); - Narrow Integration Tests â sociable tests verify logic; add a few tests per wrapper that hit real systems to catch stub drift
For testing techniques (sequences, time, events, errors), see test-patterns.md.
Building Patterns
These patterns work together:
- Output Tracking â Observe what was produced, not which methods called
- Configurable Responses â Control what Nullables return at your abstraction level
- Embedded Stubs â Stubs live in production code, maintained with wrapper
- Wrapper Composition â High-level code composes from lower-level Nullables; only leaves have stubs
Anti-Patterns
Using mock libraries â Couples tests to implementation. Don’t import sinon, jest.mock, etc. Nullables replace them.
Constructor connects to infrastructure â Constructors should perform no work. Defer connections to explicit methods. See Zero-Impact Instantiation.
Parameters at wrong abstraction level â createNull() should accept domain concepts, not implementation details:
// BAD: Leaking HTTP details
LoginClient.createNull({ httpResponse: { status: 200, body: '{"email":"x"}' } });
// GOOD: Domain level
LoginClient.createNull({ email: "user@example.com", verified: true });
Stubs in test files â Stubs belong in production code alongside the wrapper. See embedded-stubs.md.
Stub as complex as the real thing â If your stub needs significant logic, reconsider the abstraction.