Essentials / Testing

Docs Essentials

Testing

Testing a Hotwire app is like testing any other Laravel Rust app: send requests with laravel's test client and check the responses. Turbo Laravel adds a few helpers to send the requests Turbo sends and to check Turbo Streams, in the turbo::testing module. Import its prelude next to laravel's:

rust
use laravel::testing::prelude::*;
use turbo::testing::prelude::*;

Turbo HTTP Request Helpers

Acting as Turbo Visits

Turbo form submissions are marked with an Accept: text/vnd.turbo-stream.html, text/html, application/xhtml+xml header, which you may want to respond to differently, such as returning Turbo Streams instead of a redirect. Chain turbo() on a request to send it:

rust
use laravel::testing::prelude::*;
use turbo::testing::prelude::*;

#[laravel::test]
async fn creates_comments(app: Application) {
    app.post("/posts/1/comments")
        .turbo()
        .form([("body", "Hello World")])
        .await
        .assert_ok();
}

With it, request.wants_turbo_stream() returns true in the handler.

Acting as Turbo Frame Requests

You may handle requests a bit differently depending on whether they came from a Turbo Frame. To send the Turbo-Frame header, chain from_turbo_frame():

rust
use laravel::testing::prelude::*;
use turbo::testing::prelude::*;

#[laravel::test]
async fn failed_comments_render_the_form_in_the_frame(app: Application) {
    app.post("/posts/1/comments")
        .from_turbo_frame("create_comment_post_1")
        .form([("body", "")])
        .await
        .assert_redirect_to("/posts/1/comments/create");
}

Acting as Hotwire Native

When you're building a Hotwire Native app, you may want to send a request that pretends to come from a Hotwire Native client. Chain native() to send a User-Agent that mentions Hotwire Native:

rust
use laravel::testing::prelude::*;
use turbo::testing::prelude::*;

#[laravel::test]
async fn creating_trays_from_native_recedes(app: Application) {
    app.post("/trays")
        .native()
        .form([("name", "Inbox")])
        .await
        .assert_redirect_recede();
}

assert_redirect_resume() and assert_redirect_refresh() check the other two signals.

Asserting Turbo Stream HTTP Responses

assert_turbo_stream() checks that the response is a Turbo Stream response: its status is 200 and its Content-Type is text/vnd.turbo-stream.html. It returns assertions about the streams in the body:

rust
use laravel::testing::prelude::*;
use turbo::testing::prelude::*;

#[laravel::test]
async fn posting_a_comment_streams_it(app: Application) {
    app.post("/posts/1/comments")
        .turbo()
        .form([("body", "Hello World")])
        .await
        .assert_turbo_stream()
        .count(2)
        .has(|stream| stream.action("append").target("comments").see("Hello World"))
        .has(|stream| stream.action("update").target("comments_count"))
        .missing(|stream| stream.action("remove"));
}

Each check describes a stream with:

Method Matches a stream whose...
action("append") action is append
target("comments") target is comments
targets(".comment") targets is .comment
attr("method", "morph") attribute has this value
see("Hello World") content contains this text, after unescaping

has passes when a stream matches every condition, missing when none does, and count checks the number of streams. On failure, the message lists the streams the response had.

assert_not_turbo_stream() checks the opposite, such as for a request that didn't come from Turbo:

rust
use laravel::testing::prelude::*;
use turbo::testing::prelude::*;

#[laravel::test]
async fn browsers_get_a_redirect(app: Application) {
    app.post("/posts/1/comments")
        .form([("body", "Hello World")])
        .await
        .assert_not_turbo_stream()
        .assert_redirect_to("/posts/1");
}

Testing Turbo Stream Broadcasts

Fake laravel's broadcasts with laravel::broadcast::fake(), then check the Turbo Streams that were broadcast:

rust
use laravel::model::create;
use laravel::testing::prelude::*;
use turbo::testing::prelude::*;

#[laravel::test]
async fn creating_a_comment_broadcasts_it(app: Application) -> laravel::Result<()> {
    let broadcasts = laravel::broadcast::fake();

    create!(Comment { post_id: 1, body: "Hello World" }).await?;

    broadcasts.assert_turbo_stream_broadcasted(|stream| {
        stream.channel("private-posts.1").action("append").target("comments").see("Hello World")
    });

    Ok(())
}

Besides the conditions above, channel matches the channel the stream was broadcast on, with its private- or presence- prefix. Two more assertions round it out:

rust
use laravel::testing::prelude::*;
use turbo::testing::prelude::*;

let broadcasts = laravel::broadcast::fake();

// ...

broadcasts.assert_turbo_stream_broadcasted_times(2, |stream| stream.action("replace"));
broadcasts.assert_no_turbo_stream_broadcasted();