Skip to main content

Module testing

Module testing 

Source
Expand description

First-class testing support for user-land code — the missing half of the NestJS-DX promise (@nestjs/testing equivalent).

Two tools:

  • TestRequest: build a real RequestContext — through the SAME boundary pipeline production uses (body cap, provenance, tenant resolution) — without booting a server. Unit-test guards, interceptors, and handler logic directly.
  • TestServer: boot the full application on an ephemeral port for integration tests, with readiness polling built in.

§Lifetimes

Both leak their DI containers to &'static exactly like production launch does. Tests are short-lived processes; the leak is the price of keeping test contexts indistinguishable from production ones.

#[tokio::test]
async fn admin_guard_rejects_customers() {
    let ctx = TestRequest::get("/admin/users")
        .claims(serde_json::json!({"sub": "1", "role": "customer"}))
        .build()
        .await;
    assert!(RoleGuard("admin").check(&ctx).is_err());
}

Structs§

TestRequest
Builder for a production-shaped RequestContext.
TestServer
Boot the full application on an ephemeral port for integration tests.