XState Effect: Testing and errors
Test deadlines, recover from failures and supervise workflow actors.
XState v6 is in beta
Test the workflow with its real machine and provide local Effect services. Use waitFor for state assertions and join for final output.
Acknowledging an event
Import sendAndAwait from @xstate/effect/test when a test must know that one particular send was processed. The persistence example also uses it to checkpoint after each request.
It returns that mailbox item's snapshot after publication and immediate effect scheduling. Ignored events succeed with an unchanged snapshot. Concurrent sends of the same object get independent receipts. An event that completes or errors the actor returns its terminal snapshot; use join for its outcome.
Invalid and externally sent internal events fail with EventRejectedError. Stop, scope closure or completion before processing a queued send fails it with ActorStoppedError. Interrupting the waiter detaches the receipt without retracting the event. A reentrant observer stop can reject the currently processed receipt.
This helper does not await invoked tasks, background actions, child replies, future timers or global quiescence. Keep waitFor or a domain-specific completion signal for those conditions.
Existing @xstate/test SUT sessions accept async sends. Set their send function to an adapter that runs sendAndAwait and resolves to void, and read to the actor's snapshot projection. The package test suite exercises this through the real testPaths API.
TestClock
A release approval expires after 30 seconds. TestClock drives the same after transition without waiting for real time:
import { Effect } from 'effect';
import { TestClock } from 'effect/testing';
import { createEffectActor, waitFor } from '@xstate/effect';
import { createMachine } from 'xstate';
const reviewMachine = createMachine({
initial: 'pending',
states: {
pending: {
after: { 30000: { target: 'expired' } },
on: { APPROVE: { target: 'approved' } }
},
approved: { type: 'final' },
expired: { type: 'final' }
}
});
const test = Effect.gen(function* () {
const actor = yield* createEffectActor(reviewMachine);
yield* TestClock.adjust('30 seconds');
const snapshot = yield* waitFor(actor, (s) => s.matches('expired'));
return snapshot.value;
});
export const result = await Effect.runPromise(
test.pipe(Effect.scoped, Effect.provide(TestClock.layer()))
);
console.log(result); // 'expired', with no 30-second wait- Timers use Effect's
Clock, including delayed sends. waitForandjoinfail if the actor stops before reaching the expected result.- Add
waitFor's{ timeout }to bound a wait. WithTestClock, advance the clock to trigger that timeout too. - Use
inspectfor execution traces anddeadLettersfor delivery failures.
XState's path generation works on the machine itself. Execute generated paths against an actor from createEffectActor, or use testPaths() from @xstate/test.
Retry a task
Use Effect's retry combinators inside createEffectLogic for transient failures within one task. This local publisher succeeds on its third attempt:
import { Effect, Schedule } from 'effect';
import { createEffectActor, createEffectLogic, join } from '@xstate/effect';
let attempts = 0;
const publish = createEffectLogic(
Effect.suspend(() => {
attempts++;
return attempts < 3
? Effect.fail(new Error('Publisher temporarily unavailable'))
: Effect.succeed('published');
}).pipe(
Effect.retry({ schedule: Schedule.exponential('10 millis'), times: 3 })
)
);
const program = Effect.gen(function* () {
const actor = yield* createEffectActor(publish);
return yield* join(actor);
});
export const result = await Effect.runPromise(Effect.scoped(program));
console.log(result); // 'published', on attempt 3Use a machine state when retry requires a person or an event. The quick start accepts RETRY only after deployment fails.
Supervise an actor
Retry a scoped unit when a failure should create a fresh actor:
import { Effect, Schedule } from 'effect';
import { createEffectActor, createEffectLogic, join } from '@xstate/effect';
let attempts = 0;
const worker = createEffectLogic(
Effect.suspend(() => {
attempts++;
return attempts < 3
? Effect.fail(new Error('Worker disconnected'))
: Effect.succeed('complete');
})
);
const attempt = Effect.gen(function* () {
const actor = yield* createEffectActor(worker);
return yield* join(actor);
});
// Each failed attempt closes its scope before a new actor starts.
const supervised = Effect.scoped(attempt).pipe(
Effect.retry({ schedule: Schedule.exponential('10 millis'), times: 3 })
);
export const result = await Effect.runPromise(supervised);
console.log(result); // 'complete', from the third actorEach attempt stops its actor and releases its resources before the next attempt starts.
Choosing failures to retry
join reports the actor's failure, so the retry policy sees that error. A machine's error type is unknown; narrow it if only some failures should be retried. Keep Effect.scoped inside the retry, so failed actors are released after each attempt.
Retrying can repeat external operations. Use stable operation IDs or idempotent APIs when repetition could create duplicate work.
Handle typed failures
A createEffectLogic actor preserves its Effect's error type. Handle a domain failure with Effect.catchTag:
import { Data, Effect } from 'effect';
import { createEffectActor, createEffectLogic, join } from '@xstate/effect';
class PublishFailed extends Data.TaggedError('PublishFailed')<{
readonly reason: string;
}> {}
const publish = createEffectLogic(
Effect.fail(new PublishFailed({ reason: 'Release needs approval' }))
);
const program = Effect.gen(function* () {
const actor = yield* createEffectActor(publish);
return yield* join(actor).pipe(
Effect.catchTag('PublishFailed', (error) => Effect.succeed(error.reason))
);
});
export const result = await Effect.runPromise(Effect.scoped(program));
console.log(result); // 'Release needs approval'For machine workflows, expected outcomes such as declined or expired can be final states with output. join(machineActor) has an unknown error channel for unexpected machine failures.
Error reference
| Error | Raised by |
|---|---|
ActorStoppedError | waitFor when the actor stops or errors before matching; join when it stops without output. |
EffectFailure<E> | Task, stream or hosted action failure, defect or self-interruption. Its cause preserves the full Effect Cause. |
Cause.TimeoutError | waitFor with a timeout, or Effect's timeout combinators. |
ActorStoppedErrorincludesactorIdandsnapshot.snapshot.errorand invoked childonErrorevents carryEffectFailure<E>for Effect logic.ErrorFromreflects that wrapper.joinand result atoms unwrap genuineEffectFailurevalues and preserve the original Cause. Plain XState actor errors remain ordinary typed failures.EffectInterruptedErroris deprecated. InspectCause.hasInterruptsOnly(error.cause)instead.
Migrating error handlers
Effect child error handlers now receive the Cause wrapper. Replace direct access such as event.error.code with inspection of event.error.cause.
import { Cause, Effect, Option } from 'effect';
import {
createEffectActor,
createEffectLogic,
setupEffect,
waitFor
} from '@xstate/effect';
const request = createEffectLogic(Effect.fail({ code: 'NOT_FOUND' as const }));
const machine = setupEffect({ actors: { request } }).createMachine({
context: { code: '' },
initial: 'pending',
states: {
pending: {
invoke: {
src: 'request',
onError: ({ event }) => {
const failure = Cause.findErrorOption(event.error.cause);
return {
target: 'failed',
context: {
code: Option.isSome(failure) ? failure.value.code : 'UNEXPECTED'
}
};
}
}
},
failed: {}
}
});
const program = Effect.gen(function* () {
const actor = yield* createEffectActor(machine);
return (yield* waitFor(actor, (snapshot) => snapshot.matches('failed')))
.context.code;
});
export const result = await Effect.runPromise(Effect.scoped(program));
console.log(result); // 'NOT_FOUND'Use Effect.exit(join(actor)) when defects and interruption must be inspected alongside typed failures. Stopping the actor still makes join fail with ActorStoppedError.
Observe root actor errors
A root Effect actor's error is a value. Read it with join, waitFor, subscribe or the result atom. An unobserved errored actor is silent, like a failed forked fiber.
Persistence
actor.getPersistedSnapshot() records the actor's state. It does not record progress inside a running Effect: an Effect task or stream that was running when the snapshot was taken starts again from the beginning after a restore. See persisting and restoring.
Tracing
Hosted Effects run inside spans:
| Span | Work |
|---|---|
createEffectLogic | A task actor's Effect. |
createEffectStreamLogic | A stream of snapshot values. |
createEffectEventStreamLogic | A stream of parent events. |
action.<name> | A declared Effect action. |
Spans carry xstate.actor.id and xstate.actor.address. The address is the actor's /-joined path from the root, identifying its place in the actor tree.
Provide a Tracer to record and export spans.