Stately
XState v6 alpha

Utilities

Work with actors, snapshots and state values.

UtilityPurpose
waitFor(actor, predicate)Wait for a matching snapshot.
toPromise(actor)Resolve with an actor's output.
getInitialSnapshot(logic, input)Get the initial snapshot without starting an actor.
getNextSnapshot(logic, snapshot, event)Calculate the next snapshot.
mapState(...)Map one state value to another value.
SimulatedClockControl time in tests.
const finalSnapshot = await waitFor(actor, (snapshot) =>
  snapshot.matches('complete')
);

Use getInitialSnapshot(...) and getNextSnapshot(...) for pure calculations. They do not start actors or run effects. Use SimulatedClock to test delays without waiting for real time.

For example, a route loader can calculate an initial view without starting a long-lived actor. A timeout test can advance a simulated clock to the retry state.

Utilities cheatsheet

await waitFor(actor, predicate);
await toPromise(actor);
getInitialSnapshot(logic, input);
getNextSnapshot(logic, snapshot, event);

On this page