Temporalex.Testing (Temporalex v0.5.4)

Copy Markdown View Source

Workflow testing helpers built on the real Temporalex executor.

These helpers run workflow code without a Temporal server. Tests observe Temporal-visible commands, consume them in deterministic emission order, and decide when durable operations complete.

Summary

Functions

Asserts that the workflow cancelled.

Asserts that the workflow completed successfully.

Asserts that the workflow continued as new and returns the command.

Asserts that the workflow failed.

Consumes and returns the next scheduled activity command.

Consumes and returns the next raw core command.

Consumes and returns the next started timer command.

Asserts that the next command accepts the given update.

Asserts that the next command completes the given update.

Asserts that the next command rejects the given update.

Asserts that the current activation has no unconsumed commands.

Runs a query activation and asserts its successful result.

Replays the recorded activation transcript and asserts deterministic command emission.

Resolves a scheduled activity as cancelled.

Resolves a scheduled activity handle with a workflow-visible result.

Resolves a scheduled activity as failed.

Fires a started timer handle.

Runs a query activation and returns the query result.

Asserts that the workflow has not completed successfully.

Runs an activity's real implementation directly — no Temporal anywhere.

Like run_activity/4 but unwraps success and raises failures.

Delivers a signal to the workflow run.

Returns a debugging snapshot of the testing runner.

Starts a workflow run in the testing runner.

Delivers an update to the workflow run and returns an update handle.

Functions

assert_cancelled(run)

Asserts that the workflow cancelled.

assert_completed(run)

Asserts that the workflow completed successfully.

assert_completed(run, expected)

assert_continue_as_new(run)

Asserts that the workflow continued as new and returns the command.

assert_continue_as_new(run, expected_input)

assert_failed(run)

Asserts that the workflow failed.

assert_failed(run, expected)

assert_next_activity(run, opts \\ [])

Consumes and returns the next scheduled activity command.

assert_next_command(run, expected \\ nil)

Consumes and returns the next raw core command.

expected may be omitted, a command module, a full struct, or a one-argument predicate function.

assert_next_timer(run, opts \\ [])

Consumes and returns the next started timer command.

assert_next_update_accepted(run, update)

Asserts that the next command accepts the given update.

assert_next_update_completed(run, update, result)

Asserts that the next command completes the given update.

assert_next_update_rejected(run, update, reason)

Asserts that the next command rejects the given update.

assert_no_commands(run)

Asserts that the current activation has no unconsumed commands.

assert_query(run, query_type, args, expected, opts \\ [])

Runs a query activation and asserts its successful result.

assert_replay(run)

Replays the recorded activation transcript and asserts deterministic command emission.

cancel_activity(run, activity, reason, opts \\ [])

Resolves a scheduled activity as cancelled.

cancel_workflow(run, reason \\ "requested", opts \\ [])

Delivers workflow cancellation.

complete_activity(run, activity, result, opts \\ [])

Resolves a scheduled activity handle with a workflow-visible result.

fail_activity(run, activity, reason, opts \\ [])

Resolves a scheduled activity as failed.

fire_timer(run, timer, opts \\ [])

Fires a started timer handle.

query(run, query_type, args \\ [], opts \\ [])

Runs a query activation and returns the query result.

refute_completed(run)

Asserts that the workflow has not completed successfully.

run_activity(module, name, args, opts \\ [])

Runs an activity's real implementation directly — no Temporal anywhere.

This is the unit-test entry point for activity bodies (the workflow-side assert_next_activity/complete_activity kit is the complement: it tests the workflow by mocking the activity; this runs the activity itself).

assert {:ok, receipt} = Temporalex.Testing.run_activity(Activities, :charge, [100])

For activities that declare a ctx argument, a Temporalex.Activity.Context is fabricated with honest defaults (attempt: 1, the real wire type, no worker — so heartbeat/2 is a no-op) and context: merges overrides:

Temporalex.Testing.run_activity(Activities, :poll, [job],
  context: [attempt: 3, cancelled: true]
)

cancelled: true seeds the cancellation flag so Activity.cancelled?/1 and heartbeats report cancellation. Passing context: to an activity that never declares ctx raises — that test would be asserting fiction.

Returns whatever the implementation returns, untouched.

run_activity!(module, name, args, opts \\ [])

Like run_activity/4 but unwraps success and raises failures.

Activities must return {:ok, value} or {:error, reason} (the server enforces the same contract); anything else raises here too, so a test fails the same way production would.

Payments |> Temporalex.Testing.run_activity!(:charge, [100])

signal(run, name, args \\ [], opts \\ [])

Delivers a signal to the workflow run.

snapshot(run)

Returns a debugging snapshot of the testing runner.

start_workflow(workflow_module, input, opts \\ [])

Starts a workflow run in the testing runner.

The workflow is initialized immediately. Any commands emitted by the first activation are available through assert_next_activity/2, assert_next_timer/2, or assert_next_command/2.

update(run, name, args \\ [], opts \\ [])

Delivers an update to the workflow run and returns an update handle.