Skip to content

Bug: TestModel.getSimplePaths generates paths through guard-blocked transitions #5491

@Ivanok

Description

@Ivanok

XState version

XState version 5

Description

createTestModel(...).getSimplePaths() returns paths that pass through transitions whose guards evaluate to false. The guards are not being respected during graph traversal.

Example minimal reproduction:

const machine = createMachine({
  id: 'bug-repro',
  initial: 'start',          // <-- separate initial state is required to trigger the bug
  context: { allowed: false },
  states: {
    start: {
      on: { NEXT: 'idle' },
    },
    idle: {
      on: {
        // Guard blocks this until ALLOW has been dispatched first
        PROCEED: {
          target: 'done',
          guard: ({ context }) => context.allowed === true,
        },
        ALLOW: {
          target: 'idle',
          actions: assign({ allowed: () => true }),
        },
      },
    },
    done: { type: 'final' },
  },
});

const model = createTestModel(machine);
const paths = model.getSimplePaths({ toState: (s) => s.status === 'done' });

paths.forEach((p) => console.log(p.description));

Expected result

One path, requiring ALLOW before PROCEED

Reaches state "done"({"allowed":true}): xstate.init → NEXT → ALLOW → PROCEED

Actual result

Two paths, one of which passes through PROCEED before ALLOW has ever fired, ignoring the guard:

Reaches state "done"({"allowed":true}): xstate.init → NEXT → PROCEED → ALLOW → PROCEED
Reaches state "done"({"allowed":true}): xstate.init → NEXT → ALLOW → PROCEED

Reproduction

https://stackblitz.com/edit/github-cguebm7g?file=src%2Fmain.ts

Additional context

I have also prepared the same machine with xstate 4 and @xstate/test, which illustrates the expected result: https://stackblitz.com/edit/github-cguebm7g-hv4u2asb

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions