Skip to main content

Module system

Module system 

Source
Expand description

System tests: tests of the whole app, which run as no service.

A test in a service runs as that service, alone: a call to another service is not answered, and what it publishes nothing handles. A test in a system test suite — a crate whose directory holds an encore.systest.json — runs against the app instead, in its test session: each service starts when something first reaches it, as under encore run, with the secrets and configs configured for tests and data that is the session’s own.

ⓘ
use encore::testing::system;

static BILLING: Database = Database::named("billing");

#[encore::test]
async fn placing_an_order_charges_the_card() -> Result<(), Box<dyn std::error::Error>> {
    let order = orders::place(&PlaceParams { sku: "tee".into(), qty: 2 }).await?;
    // The order's event has been handled by billing's subscription.
    system::settled().await?;

    let row = BILLING
        .query_one("SELECT amount FROM charges WHERE order_id = $1", &[&order.id])
        .await?;
    assert_eq!(row.get::<_, i64>(0), 4000);
    Ok(())
}

A system test may use any resource the app declares, bound with named or through a service’s own declaration, and call any service.

Each test has data of its own: an instance of every database, Redis database and bucket, which starts as a service’s test’s does, the databases migrated and the rest empty. What the services do for the test they do on that data, whichever of them it is, so a test reads what they wrote for it and nothing of another test’s, and tests run side by side.

A test can start from a Fixture instead, as a service’s test can. Its setup calls the app, and what the services made of that, in every database, bucket and Redis database, once what the setup set in motion has finished, is what each test that loads the fixture gets a copy of.

What the app does for no request, as a Worker’s loop or a job that finds its work in a table, is done on the data the services share, which a test does not see. A suite that tests such work says "data": "shared" in its encore.systest.json: its tests then share that data too, and each other’s.

What a test publishes is sent, and each subscription handles it in the service that declares it. settled waits for that: there is nothing to deliver.

Each test has an id, which travels with every call and message it makes, and with what those cause in turn, so that what the app did for the test can be told from the rest. A message, or a call to a service, made on a thread no test can be told from panics: it would carry no test.

A test that drives the app’s frontends asks for a browser: a headless Chromium Encore runs for the test, whose pages reach the app in the test session and are recorded.

Run the tests with encore test, or with cargo test in the suite’s directory.

Structs§

Browser
The current test’s browser: see browser. Cheap to clone, and every clone is the same browser.
BrowserOptions
How a test wants its browser: see browser_with.
Deliveries
The deliveries of a Telemetry, from Telemetry::deliveries.
Event
An event of a span.
Link
A link to another span.
Log
A line a service logged, or something Encore captured of a request.
LostContext
A call between services that did not carry its request’s context, and so not the test: see Telemetry::lost_context.
Publishes
The publishes of a Telemetry, from Telemetry::publishes.
Queries
The queries of a Telemetry, from Telemetry::queries.
Requests
The requests of a Telemetry, from Telemetry::requests.
SettleOptions
The options of settled_with.
Span
One span.
Telemetry
What a test session recorded for one test.
TelemetryOptions
The options of telemetry_with.

Enums§

Kind
A span’s kind.
Record
When what a test’s browser recorded is kept.
Status
A span’s status.
What
What a span is of, with what a test asks of one.

Functions§

browser
The current test’s browser: a headless Chromium Encore runs for the test, for a tool of the test’s own to drive. It is started the first time the test asks, and ends with the test.
browser_with
browser, as options say. They are the first ask’s to say: a test has one browser, and asking again gives the same one.
headers
The headers that make a request part of the current test, for a client of the test’s own: the test’s id, as the Request Context field Encore carries it in, and the test’s span for the request to continue.
settled
Waits until nothing the test set in motion is still running or queued: no request in flight to a service or through a gateway, and no message waiting, being handled or due a retry on a subscription. For at most 30 seconds; see settled_with.
settled_with
settled, as options say. The error of an app that did not settle says what was still under way.
telemetry
What the app recorded for the current test, once it has settled: every span of what the test caused — in the services it reached, in the requests between them, in the deliveries of what was published for it — and the lines logged in them.
telemetry_with
telemetry, as options say: of what has been recorded by now, without waiting for the app to settle, or waiting for at most a timeout.
url
The URL of the gateway or frontend name in the test session, for a client of the test’s own, such as a WebSocket client. A frontend starts when something first connects to it.