Testing
Boot the real DI graph in-process, drive every transport in cargo test, override providers with first-class support.
nest-rs-testing boots the real AppBuilder (its four phases, seeds → collect →
factories → register, then the access-graph check) in-process, configures an
HttpTransport without a socket, and hands you TestClient, its own
in-process client over the stack the server runs — the edge, the router, the
guards and your handlers. GraphQL, OpenAPI, and MCP ride the same client
because they self-mount as HTTP endpoints.
The principle: a test is the truth of what production does. No mocked DI graph, no separate test wiring.
Three categories, Rust’s standard model plus the app e2e:
- Unit tests —
#[cfg(test)] mod testsinsidesrc/. Pure logic next to the code under test, with private-item access and no DI boot. - Integration tests —
tests/integration/main.rs, a directory and never a flattests/<x>.rs. The crate’s public API, exercised as an external consumer would. Shared fixtures sit in the suite’smain.rs, beside itsmodlist. - End-to-end tests —
TestAppboots the realApiModuleon a throwaway database and a signing key of the test’s own. The route table, the DI graph, the access-graph check, authn/authz, and the data path all run.
| Where | What it tests | Boots? |
|---|---|---|
crates/<crate>/src/**.rs (#[cfg(test)]) |
In-file logic, private items | no |
crates/<crate>/tests/integration/main.rs |
Crate’s public API | no |
crates/<crate>/tests/e2e/main.rs |
Crate’s public API against live infra | tailored |
apps/<app>/tests/e2e/main.rs |
The app’s real top-level module | ✓ real |
Install
Section titled “Install”cargo add --dev nest-rs --features testingA dev-dependency — the harness only compiles under cargo test, never into
your release binary. TestApp, the HTTP half, rides the http feature your
app already enables. A crate built without http — a worker, a queue driver —
keeps its suite headless too: it boots through HeadlessApp::builder(), and
its dev-dependency leaves the default features off:
cargo add --dev nest-rs --no-default-features --features queue,testingPicking the right tool
Section titled “Picking the right tool”The three categories are the where. The which depends on the risk you carry. nestrs ships techniques for each — pick what matches, skip what doesn’t:
| Risk you carry | Technique it earns |
|---|---|
| Wiring (DI graph, routes mounted, guards bound) | E2E — TestApp boots the real module tree |
Security policy regressions (condition_for, permitted_fields, can) |
Policy tests — pure unit tests on the policy types |
| Failure modes (401 / 403 / 400 / rollback) | Negative-path tests in the app’s e2e |
| Pure logic — calculations, conversions, validators | Unit tests next to the code |
| Edge cases in parsers and validators | Property-based testing with proptest |
| API contract drift (OpenAPI, GraphQL SDL) | Snapshot tests on /api-json and schema.graphql |
| A framework crate’s public API | Integration tests in the crate’s tests/ |
A small CRUD feature with no calculation and no custom policy can ship with zero unit tests, zero integration tests, and full coverage through its app’s e2e. That is a valid outcome — write what the risk earns, skip what it doesn’t.
Run it
Section titled “Run it”$ nestrs run test integration # in process, no database Compiling api v0.1.0 Finished `test` profile in 12.4stest result: ok. 142 passed in 0.21s$ nestrs run test e2e # e2e — Postgres requiredtest create_then_list_users ... oktest result: ok. 14 passed in 4.31s$ nestrs run test cov # full suite with coverage (llvm-cov)cov shells out to the LLVM tools. The CLI’s first-run bootstrap installs
cargo-llvm-cov, and rustup installs the llvm-cov / llvm-profdata it drives
on the first run, from the llvm-tools-preview component the scaffold’s
rust-toolchain.toml pins. A toolchain without rustup (Homebrew, a distro
package) ignores that file: point LLVM_COV / LLVM_PROFDATA at its own copies
instead.
One recipe per kind, named for the suite it runs: unit the #[cfg(test)]
modules, integration the tests/integration suites — in process, no
database — e2e the tests/e2e suites against Postgres and Redis, and doc
the examples in doc comments. Bare nestrs run test runs every kind, and
nestrs run ci runs the lint before it.
What “done” requires
Section titled “What “done” requires”A wiring bug does not surface in a unit test.
- Every app ships a
tests/e2e/main.rsbooting its real top-level module — it proves the route table, the DI graph, the access-graph check, the authn/authz layers, and the data path are all wired. - HTTP/GraphQL changes are confirmed against a running binary:
nestrs run dev <app>plus acurlon the affected endpoint. The e2e suite proves the wiring; the curl proves the wire shape. - No DB mocking in e2e tests — a real database per test via
ephemeral_database, created on the server<PREFIX>_SEAORM__URLpoints at and dropped after the app. Unit tests of pure logic need no DB.
Reference
Section titled “Reference”crates/nest-rs-testing/—TestApp,TestAppBuilder,EphemeralDatabase, the in-process HTTP client.apps/api/tests/e2e/main.rsandapps/auth/tests/e2e/main.rs— the live baseline tests.- The Rust Book, ch. 11.3 and the Cargo Book on integration tests — the underlying conventions.
In this section
Section titled “In this section”Basics
Section titled “Basics”- Unit tests — pure logic next to the code, no DI boot.
- Integration tests — a crate’s public API in process, no database and no network.
- End-to-end tests — the real
ApiModuleagainst live Postgres, Redis and S3.
All options
Section titled “All options”- Driving the app — a bearer the app verifies, the HTTP client, a GraphQL subscription with no socket.
- Provider overrides — a double in place of a provider, before anything is built from it.
- Headless apps — a worker on a Redis database of its own, the outbox relay, the way down.
- Negative-path tests — auth refused, cross-tenant denied, validation failed, rollback verified.
- Policy tests — asserting on
Ability::permitted_fieldsandcondition_fordirectly, because a regression there is a leak.
Going further
Section titled “Going further”- The Publish workspace — the exemplar apps these suites ship against.
- CLI —
nestrs run test, and the nextest gate that keeps e2e out of the fast loop. - Overriding in tests — hermetic configuration in both suites.