On this page:
3.1 Generator Helpers
make-mock-sse
make-recording-sse
3.2 Event Struct
sse-event

3 Testing🔗ℹ

 (require datastar/testing) package: datastar-lib

Test SSE-producing handlers without opening a real HTTP connection. This module provides mock generators and parsed event values for assertions. Require it separately from datastar.

3.1 Generator Helpers🔗ℹ

Create mock sse? values for unit tests and inspect emitted output.

procedure

(make-mock-sse)  
sse? (-> string?)
Creates a mock sse? generator that works with all the normal send functions (patch-elements, patch-signals, etc.) but doesn’t touch the network. Returns two values: the generator, and a thunk that returns all the SSE text that has been sent through it so far.

> (define-values (sse get-output) (make-mock-sse))
> (patch-elements/xexprs sse '(div ((id "x")) "hi"))
> (get-output)

"event: datastar-patch-elements\ndata: elements <div id=\"x\">hi</div>\n\n"

procedure

(make-recording-sse)  
sse? (-> (listof sse-event?))
Like make-mock-sse, but instead of returning raw text, the retrieval thunk returns a list of sse-event structs, one per event sent.

The events are parsed from the same SSE text that would go over the wire, so they reflect exactly what a real client would receive, including serializer-side omission of default wire fields (for example mode 'outer, namespace 'html, useViewTransition false, onlyIfMissing false, and default retry).

> (define-values (sse2 get-events) (make-recording-sse))
> (patch-elements/xexprs sse2 '(div "test"))
> (patch-signals sse2 (hash 'x 1))
> (define events (get-events))
> (length events)

2

> (sse-event-type (first events))

"datastar-patch-elements"

> (sse-event-type (second events))

"datastar-patch-signals"

3.2 Event Struct🔗ℹ

Use sse-event values to assert exact wire-level SSE output in tests.

struct

(struct sse-event (type id retry data-lines)
    #:transparent)
  type : string?
  id : (or/c string? #f)
  retry : (or/c exact-nonnegative-integer? #f)
  data-lines : (listof string?)
A parsed SSE event. Transparent, so check-equal? works on it directly.

  • type – event type string (e.g. "datastar-patch-elements").

  • id – event ID, or #f if none was set.

  • retry – retry duration in milliseconds, or #f if the default was used (default-equivalent retry is omitted by the serializer).

  • data-lines – list of data line contents without the data: prefix. For example, '("elements <div>hello</div>").