Real-Dependency E2E
Mocks hide the bugs that reach production.
The bug that reaches production is the one the test never exercised. Mocked dependencies pass mocked tests. The migration that breaks production breaks the live database, the live queue and the live upstream. The only test that catches it is the one that touches them.
Opinion
I've cleaned up too many post-mortems where the test suite was green and the database migration shipped a broken column. The unit tests passed because the mock returned what the mock was told to return. The integration tests passed because they shared the same fakes. The bug was in the rename that the ORM accepted and the migration didn't. The fix is the Kent C. Dodds line written down twenty times in the back of the runbook: write an E2E test that uses the DB itself.1 Same instinct generalises (real queue, real cache, real upstream API) the moment the dependency is part of the production data path.
Most teams misread the unit-vs-E2E inversion. Unit tests should mock everything outside the unit, the discipline TS7 Test Isolation enforces. E2E tests should mock nothing inside the system, the discipline TS4 enforces. The two are paired tenets rather than contradictions. The boundary between the layers decides what counts as the unit. The rules either side of it are different by design.
Determinism is the operational discipline that makes the rule survive contact with CI. The database is rebuilt from a fixed seed before each run.9 The queue starts from a known commit; the upstream is pinned to a tagged release. Fowler's Eradicating Non-Determinism in Tests2 is the playbook; Google's “hermetic servers” pattern3 is the architectural pattern. The dependency itself is sealed inside a boundary the test owns rather than the world. The third-party SaaS that won't cooperate (a payment processor, a card-present terminal) gets a contract test plus a recorded interaction and the narrow exception is documented as such, not as a green light to re-introduce mocks elsewhere.
Copy a note and link
Grab this short comment and drop it into a PR comment or an LLM chat to prompt the right change.
End-to-end tests run against the dependencies themselves: real database, real queue, real API. Mocked dependencies pass mocked tests. Only the thing itself catches the migration that breaks production. Determinism comes from rebuilding state per run, not from replacing the dependency. The unit-tier mocks-everything contract is preserved. The E2E tier inverts it. /tenet/real-dependency-e2e/TS4
AI eyes only
Rule: E2E tests touch real dependencies. Unit tests mock outside the unit; E2E tests mock nothing inside the system.
Reject: vi.mock of any internal service inside an E2E test. Reject: an E2E test that passes against an in-memory fake. Reject: collapsing the unit and E2E tiers because the unit tier is easier to write.
Generate: E2E tests that run against the database itself, real HTTP, real filesystem. Migration fixtures, seed loaders and teardown helpers belong to the test setup.
Diagnostic: take the database offline. If the E2E suite still passes, it is not E2E. The tier is mislabelled.
Why?
- Catches the migration that breaks production. The ORM accepts the column rename. The database doesn't. Only a test that runs against the database itself surfaces the mismatch in CI rather than at deploy.
- Surfaces network-shaped failures: the queue that times out, the cache that returns stale data, the upstream that rate-limits at 100 RPS. None of these reproduce against a fake.
- The hermetic boundary keeps the apex of the pyramid affordable. Service containers seed from a known commit. The suite stays minutes, not hours, because the run is engineered for determinism.
- Pairs cleanly with TS7 Test Isolation. The two rules carve the unit-vs-E2E boundary at the right altitude. Both apply, both stay honest about their layer.
- Enables structural E2E. With the data real, the locator-side discipline of TS5 Structural Assertions can run against the live build: sitemap discovery, role-tree assertions, the works.
- Stops coding agents from collapsing the E2E tier into the unit tier. A clear rule in AGENTS.md plus a CI workflow with real services keeps the model writing fixtures and seed scripts rather than mocks.
- The narrow third-party carve-out forces honesty. Each unmockable dependency is documented with its contract test, recorded interaction, and rationale, not a green light to mock elsewhere.
Origins
The “real database in E2E” line has three load-bearing primary sources. Kent C. Dodds's 2020 tweet1 is the snappiest formulation: write an E2E test that uses the DB itself. Martin Fowler's Eradicating Non-Determinism in Tests2 set out the production-fidelity argument: a test that doesn't exercise production-shaped infrastructure cannot catch production-shaped failures. Google's “hermetic servers” series on the Testing Blog3 formalised the architectural pattern: the backend itself, sealed behind a hermetic boundary the test owns.
The idea generalises past the database. Justin Searls's “Don't mock what you don't own” line6 is the boundary half: the third-party SaaS gets a recorded interaction or a contract test, but the dependencies you do own go in real. Software Engineering at Google's Test Sizes taxonomy7 places real DB, real queue, real network in “Large” tests as the fidelity ceiling of the pyramid. The Postgres-specific idea in the original Prickles standard is one instance of a broader rule that covers every real dependency in the production data path.
Vladimir Khorikov's classical school chapter8 documents the in-process variant: integration tests mock across process boundaries. Touch the database itself in-process. The chapter is the bridge between the unit-mock-everything discipline and the E2E-mock-nothing discipline. Both apply, at different tiers, against the same architectural backbone. Hermetic but realistic3 is the operational shorthand.
In the Prickles canon, TS4 is one half of a paired-tenets contrast. TS7 Test Isolation is the inverse rule at the unit tier: the unit touches nothing outside the unit. The two rows together draw the unit-vs-E2E boundary cleanly: the testing-pillar equivalent of the F3 DRY ↔ S2 Wait for Three contrast in style. The contradiction is only apparent: two layers, both true.
Quotes
Write an E2E test that uses the real DB.
In order to get tests to run reliably, you must have clear control over the environment in which they run, so you have a well-known state at the beginning of the test. … There are a couple of ways to get isolation - either always rebuild your starting state from scratch, or ensure that each test cleans up properly after itself.
If you can start up the entire server on a single machine that has no network connection AND the server works as expected, you have a hermetic server!
Use real instances of managed dependencies; replace unmanaged dependencies with mocks.
Evidence
18 external sources, ranked by author authority. The first five are the canon; expand to see the rest, including the qualifiers and the named opposers. Each links out to its primary source.
- 01The snappiest formulation. The mainstream JavaScript-ecosystem articulation of TS4’s rule, in one sentence.
- 02The production-fidelity argument paired with the determinism playbook. Rebuild starting state from scratch for each test environment so results are deterministic.
- 03The architectural pattern: real backends sealed behind a hermetic boundary the test owns. The phrase “hermetic but realistic” is the operational shorthand.
- 04FAANG-scale evidence. The Test Sizes taxonomy explicitly puts “real DB”, “real network between processes” in Large tests as the fidelity ceiling of the pyramid.
- 05Test Sizes (Google Testing Blog)SupportsThe Small / Medium / Large taxonomy. Large tests use real backends. The other tiers don’t. The classification predates the SwE-at-Google book and underwrites the same line.
- 06The classical school’s defence of real-DB integration. Mocks for unmanaged dependencies (across process boundaries) only. Touch the database itself in-process.
- 07Don’t mock what you don’t ownSupportsThe boundary half. Third-party dependencies get contract tests. The dependencies you do own go in real. The idea TS4 generalises past the database.
- 08The primary tooling reference. Real database, real cache, real queue spun up alongside the workflow run. The canonical CI pattern for hermetic real-dependency E2E.
- 09Drizzle ORM CI workflowSupportsIndustrial use of the pattern. The ORM’s own CI runs MySQL, Postgres, SQLite, and Turso service containers. The test suite’s realism is part of the product’s contract.
- 10The CI angle. Service containers, seeded SQL, deterministic teardown. The post is the most-linked operational primer for TS4’s database-half on JavaScript stacks.
- 11`WebApplicationFactory` against real DB providers as Microsoft’s recommended pattern. The .NET ecosystem’s articulation of the same rule.
- 12Code Quality at StripeSupportsProduction-realistic test environments. Stripe’s test environments mirror production load shape. The fidelity gain pays off because the cost of a payment bug is asymmetric.
- 13Backstage Test StrategySupportsSandboxed-but-real DB for plugin tests. Backstage’s ecosystem runs TS4 by default for the plugin contract. The database is real, the boundary is sealed.
- 14Test Pyramid in PracticeQualifiesThe pyramid idea. Real-dependency tests live at the apex. The bulk of the suite stays unit-shaped. Read it for the cost angle; TS4 is the apex rule, not the base.
- 15Test Pyramid (bliki)QualifiesThe canonical pyramid. Sets the apex where TS4 lives. Sharpens the cost claim the reply has to address.
- 16Agile Testing: Q4 quadrantSupportsQ4 (technology-facing tests that critique the product) is the quadrant home for production-fidelity testing. The agile-testing canon’s placement of TS4’s discipline.
- 17Integrated Tests Are A ScamOpposesThe principal published objection. Integrated tests give false confidence. They pass while internal contracts break. The reply addresses by adding contract testing alongside, not in place of, E2E.
- 18Cal.com CI workflowSupportsIndustrial Next.js + database + Playwright stack. The Cal.com workflow is the most-cited ready-to-copy reference for TS4 in a production-grade JavaScript codebase.
Eighteen sources, three stances. The supports are the canon: Kent C. Dodds' real-DB essay, Fowler on Eradicating Non-Determinism, the Google “hermetic servers” line and the SwE at Google Test Sizes taxonomy. The qualifiers further down carve out the cost angle: real-dependency E2E is the apex of the pyramid rather than the base. The opposers hold the steelman the reply has to address.
Enforcement
Apply these rules in .github/workflows/e2e.yml. The full enforcement across every tenet lives on the implementation page.
| Rule | Tool | Catches |
|---|---|---|
| services: container | GitHub Actions service containers | in-memory database swaps. The services block in the workflow runs the database, queue or cache itself and the test process talks to it over the same driver and port shape as production. |
| playwright webServer + baseURL | Playwright | tests that bypass the deployed build. The webServer block makes Playwright start the application itself and the baseURL points at the assembled system rather than a fixture. |
| playwright test.use({ trace }) | Playwright | missing diagnostics on real-dependency failures. Trace mode captures the request log and DOM snapshots so a failed E2E run is debuggable from the report. |
| @testcontainers/node | @testcontainers/node | local-dev parity issues. Testcontainers spins up the same image CI uses; the developer’s laptop and the workflow run agree on the dependency surface. |
| pact provider verification | Pact | the third-party carve-out. The contract test runs against the vendor’s sandbox; the recorded interaction stands in for the dependency the team didn’t buy. |
| no in-memory database in E2E | ESLint custom rule (recommended) | better-sqlite3, :memory:, or in-process database imports inside e2e/ directories. The lint rule fails the PR before the suite runs. |
.github/workflows/e2e.ymlconfiguration snippet
name: e2e
on: [pull_request]
jobs:
e2e:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app_test
ports: ['3306:3306']
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5
redis:
image: redis:7
ports: ['6379:6379']
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
- run: pnpm install --frozen-lockfile
- run: pnpm migrate --url ${{ env.DATABASE_URL }}
- run: pnpm seed
- run: pnpm exec playwright install --with-deps
- run: pnpm test:e2e
env:
DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
REDIS_URL: redis://127.0.0.1:6379Apply these rules in .github/workflows/e2e.yml. The full enforcement across every tenet lives on the implementation page.
| Rule | Tool | Catches |
|---|---|---|
| services: container | GitHub Actions service containers | in-memory database swaps. The services block in the workflow runs the database, queue or cache itself and the test process talks to it over the same driver and port shape as production. |
| playwright webServer + baseURL | Playwright | tests that bypass the deployed build. The webServer block makes Playwright start the application itself and the baseURL points at the assembled system rather than a fixture. |
| playwright test.use({ trace }) | Playwright | missing diagnostics on real-dependency failures. Trace mode captures the request log and DOM snapshots so a failed E2E run is debuggable from the report. |
| @testcontainers/node | @testcontainers/node | local-dev parity issues. Testcontainers spins up the same image CI uses; the developer’s laptop and the workflow run agree on the dependency surface. |
| pact provider verification | Pact | the third-party carve-out. The contract test runs against the vendor’s sandbox; the recorded interaction stands in for the dependency the team didn’t buy. |
| no in-memory database in E2E | ESLint custom rule (recommended) | better-sqlite3, :memory:, or in-process database imports inside e2e/ directories. The lint rule fails the PR before the suite runs. |
.github/workflows/e2e.ymlconfiguration snippet
name: e2e
on: [pull_request]
jobs:
e2e:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app_test
ports: ['3306:3306']
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5
redis:
image: redis:7
ports: ['6379:6379']
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
- run: pnpm install --frozen-lockfile
- run: pnpm migrate --url ${{ env.DATABASE_URL }}
- run: pnpm seed
- run: pnpm exec playwright install --with-deps
- run: pnpm test:e2e
env:
DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
REDIS_URL: redis://127.0.0.1:6379Apply these rules in playwright.config.ts. The full enforcement across every tenet lives on the implementation page.
| Rule | Tool | Catches |
|---|---|---|
| services: container | GitHub Actions service containers | in-memory database swaps in CI. |
| playwright webServer | Playwright | tests that bypass the deployed build. |
| cypress.config: baseUrl + setupNodeEvents | Cypress | the equivalent escape hatch in Cypress projects. setupNodeEvents runs migration and seed tasks before the E2E suite hits the real services. |
| @testcontainers/node | @testcontainers/node | local-dev parity issues. |
| wiremock | WireMock | third-party HTTP dependencies that genuinely cannot be run hermetically. Replays recorded interactions against the live test rather than a unit-tier mock. |
playwright.config.tsconfiguration snippet
import { defineConfig } from '@playwright/test';
export default defineConfig({
testDir: 'e2e',
fullyParallel: false,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
reporter: [['list'], ['junit', { outputFile: 'reports/e2e-junit.xml' }]],
use: {
baseURL: process.env.E2E_BASE_URL ?? 'http://localhost:3000',
trace: 'retain-on-failure',
},
webServer: {
command: 'pnpm start',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
}
});Apply these rules in pyproject.toml. The full enforcement across every tenet lives on the implementation page.
| Rule | Tool | Catches |
|---|---|---|
| MySqlContainer / PostgresContainer | testcontainers-python | in-memory database swaps. The container starts the real image, runs migrations, and tears down per session; the suite agrees with CI’s service container. |
| RabbitMqContainer / KafkaContainer | testcontainers-python | fake queues. Real broker, real protocol, real timing: the failure modes the unit-tier fake hides. |
| @pytest.mark.real_db | pytest | tests that drop their real-DB requirement unnoticed. The marker makes the dependency explicit and runnable as a separate selection in CI. |
| pytest-docker | pytest-docker | the Compose-based variant. Useful when the test session needs more than one container and Docker Compose is the local-dev choice. |
| pact-python | Pact (Python) | third-party HTTP dependencies. Consumer-driven contract tests cover the boundary the unit-tier mock used to fake. |
| Alembic upgrade head in fixture | Alembic | schema drift between migrations and the real-DB E2E run. The fixture runs every committed migration before the suite starts; the database state matches the deploy. |
pyproject.tomlconfiguration snippet
[tool.pytest.ini_options]
addopts = "-v"
testpaths = ["tests/e2e"]
markers = [
"real_db: requires the seeded MySQL service container",
"real_queue: requires the seeded queue service container"
]Apply these rules in pom.xml. The full enforcement across every tenet lives on the implementation page.
| Rule | Tool | Catches |
|---|---|---|
| @Container + MySQLContainer | Testcontainers (Java) | in-memory database swaps. The annotation starts the real image as part of the JUnit lifecycle; the test agrees with the deploy on driver, dialect, and migration shape. |
| @DynamicPropertySource | Spring Boot | hard-coded database URLs in application-test.yml. The Spring context picks up the container’s host:port at runtime; the application binds to the real service automatically. |
| RabbitMQContainer / KafkaContainer | Testcontainers (Java) | fake queues in JVM-stack tests. Real brokers, real protocol, real consumer-group timing. |
| @PactConsumerTest | Pact-JVM | the third-party carve-out for JVM clients. Consumer-driven contract tests record interactions and verify them against the vendor’s pact broker. |
| Flyway migrate (test profile) | Flyway | schema drift between migrations and the real-DB E2E run. Flyway runs every committed migration before the suite starts. |
| JUnit 5 @Tag("e2e") | JUnit 5 | missing CI separation. Tags split the suite into unit / integration / e2e tiers; the workflow runs each tier with its own service-container topology. |
pom.xmlconfiguration snippet
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>1.19.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>au.com.dius.pact.consumer</groupId>
<artifactId>junit5</artifactId>
<version>4.6.5</version>
<scope>test</scope>
</dependency>
</dependencies>Apply these rules in composer.json. The full enforcement across every tenet lives on the implementation page.
| Rule | Tool | Catches |
|---|---|---|
| MySqlContainer / PostgresContainer | Testcontainers PHP | in-memory database swaps in PHPUnit suites. The container starts the real image; the test driver talks to it over PDO with the same dialect as the deploy. |
| Docker Compose service block | Docker Compose | missing local-dev parity. docker compose up -d mysql redis mirrors the CI workflow; the developer’s laptop and the GitHub Actions run agree on the dependency surface. |
| PHPUnit #[Group('e2e')] | PHPUnit | missing CI separation. Group attributes split unit / integration / e2e tiers; CI runs each with its own service topology. |
| pact-php consumer test | pact-php | third-party HTTP dependencies in PHP clients. Consumer-driven contract tests verify against the vendor’s pact broker. |
| Phinx migrate -e testing | Phinx | schema drift between migrations and the real-DB E2E run. Phinx (or the Laravel migrate equivalent) runs every committed migration before the suite starts. |
| Behat MinkExtension wd_host | Behat | tests that bypass the deployed build. The Mink extension drives the real browser through Selenium / Chromedriver; the scenario hits the assembled system end-to-end. |
composer.jsonconfiguration snippet
{
"require-dev": {
"phpunit/phpunit": "^13.3",
"testcontainers/testcontainers": "^0.4",
"pact-foundation/pact-php": "^9.0"
}
}AI rules
.cursor/rules/ts4-real-dependency-e2e.mdc--- alwaysApply: true --- ## Prickles TS4: Real-Dependency E2E Run E2E tests against the real dependencies the production system touches: real database, real message queue, real API surface. Seal them inside a hermetic boundary and never replace them. Mocks belong below the E2E line. Unit tests mock everything outside the unit; E2E tests mock nothing inside the system under test. The two layers do different jobs. Determinism comes from rebuilding state, not from replacing the dependency. Seed the database fresh per run. Bring up the queue from a known commit. Pin the upstream version. If a dependency cannot be run hermetically (third-party SaaS, payment processor sandbox, real card-present terminal), wrap it in a contract test plus a recorded interaction. Reach for in-process replacements only when the third party itself sells one.
Repo layout, CI, and ESLint wiring for these paths live on /implementation, not repeated on every tenet.
Counter-argument
The strongest pushback is J. B. Rainsberger's4: integrated tests give false confidence. They can pass while internal contracts are broken. The suite reports green and the next unrelated change perturbs the wrong cell. The test-pyramid school5 sharpens the cost angle: real-dependency E2E is slow, flaky, and expensive at scale. The bulk of the suite has to be unit-shaped or the pipeline grinds to a halt. A third voice argues that “real DB” in CI is still not production DB (different load, different data volumes, different network topology) so the fidelity gain is theatre.
Counter-argument retort
Rainsberger's objection4 is conceded inside its scope and refused outside it. Yes, the integrated test can pass while internal contracts are broken. The answer is to add contract tests at the boundaries, not to remove the E2E that catches the migration the contract test can't see. Both apply. Contract testing covers the interface; E2E covers the assembled system. The two coverages are orthogonal, not substitutes.
The test-pyramid objection5 is a quantity claim disguised as a fidelity claim. The pyramid says the bulk of the suite has to be unit-shaped because integration tests are slow; TS4 doesn't argue against that. TS4 is about the few E2E tests you do run: it doesn't say everything should be E2E. The pyramid stays; TS4 is the rule for the apex. Fowler's own non-determinism playbook2 handles the slow-and-flaky concern: rebuild state from a fixed seed. Pin the upstream. Seal the boundary. The pipeline does not have to grind to a halt. It does have to be engineered.
The “real DB is theatre” objection inverts itself the moment a migration breaks. Production fidelity is a continuum: the CI MySQL that the application reaches via the same driver and the same SQL surface catches the migration that the ORM accepted and the database didn't. The objection is correct that CI is not production. It's wrong that half-fidelity is no fidelity. The failure mode lives in the half CI does reproduce.
The narrow residue is the third-party dependency that genuinely cannot be run hermetically. The card-present terminal, the upstream SaaS that bills per request, the certificate authority that signs against a real HSM. For those, write a contract test against the vendor’s sandbox or staging endpoint, record the interaction with a tool the team owns (Pact, WireMock, MSW for HTTP), and document the gap explicitly. That's not a licence to mock elsewhere; it's an explicit, audited carve-out for a dependency the team didn't buy and can't replicate.
The discipline reduces to two lines on the wall: real dependency or contract test, never both-mocked. The unit tier mocks everything outside the unit. The E2E tier mocks nothing inside the system. Same operating principle as TS7 on its mirrored surface.
Notes
- [1]Kent C. Dodds: Tweet, 2020-04-20: “Write an E2E test that uses the real DB.” The shortest formulation of the TS4 rule and the one the original Prickles standard cited.
- [2]Martin Fowler: Eradicating Non-Determinism in Tests (martinfowler.com, 2011). The production-fidelity argument paired with the determinism playbook: rebuild starting state from scratch per run.
- [3]Google Testing Blog: “Hermetic Servers” (testing.googleblog.com, 2012). The architectural pattern: real backends sealed behind a hermetic boundary the test owns.
- [4]J. B. Rainsberger: “Integrated Tests Are A Scam” (thecodewhisperer.com, 2010). The principal counter-argument. Integrated tests give false confidence. The reply adds contract testing alongside, rather than removing E2E.
- [5]Mike Cohn / Martin Fowler: Cohn’s Succeeding with Agile (2009) introduced the test pyramid, and Fowler’s 2012 bliki entry codified it. The cost angle the reply addresses by treating TS4 as the apex rule.
- [6]Justin Searls: “Don’t mock what you don’t own” idea (testdouble.js README, 2014). The boundary half: third-party dependencies get contract tests. The dependencies you do own go in real.
- [7]Titus Winters, Tom Manshreck & Hyrum Wright: Software Engineering at Google (O’Reilly, 2020), ch. 11 Testing Overview. The Test Sizes taxonomy places real DB, real queue, real network in “Large” tests as the fidelity ceiling of the pyramid.
- [8]Vladimir Khorikov: Unit Testing: Principles, Practices, and Patterns (Manning, 2020), ch. 8 Why integration testing? The classical school’s defence of real-DB integration: mocks for unmanaged dependencies only.
- [9]GitHub Actions documentation: Service Containers (docs.github.com, 2020+). Real database, real cache, real queue spun up alongside the workflow run. The canonical CI pattern for hermetic real-dependency E2E.