import * as fc from 'fast-check'; import { createMachine, initialTransition } from 'xstate'; import { assertTestCoverage, fastCheckAdapter, formatTestCoverage, propertyTest, testCoverageToJSON, testPaths } from '../src/index.ts'; const checkoutMachine = createMachine({ id: 'checkout', initial: 'cart', states: { cart: { initial: 'items', states: { items: {}, empty: {} }, on: { CHECKOUT: { target: 'shipping' } } }, shipping: { initial: 'editing', states: { editing: {}, invalid: {} }, on: { SUBMIT_SHIPPING: ({ event }) => { if (!(event as { name?: string }).name) { return { target: '.invalid' }; } return { target: 'payment' }; }, BACK: { target: 'cart' } } }, payment: { initial: 'editing', states: { editing: {}, invalid: {}, declined: {} }, on: { SUBMIT_PAYMENT: ({ event }) => { if (!(event as { card?: string }).card) { return { target: '.invalid' }; } return { target: 'review' }; }, BACK: { target: 'shipping' } } }, review: { on: { PLACE_ORDER: ({ event }) => { if ((event as { outcome?: string }).outcome === 'declined') { return { target: 'payment.declined' }; } return { target: 'confirmed' }; }, BACK: { target: 'payment' } } }, confirmed: { on: { START_OVER: { target: 'cart' } } } } }); const happyPath = [ { type: 'CHECKOUT' }, { type: 'SUBMIT_SHIPPING', name: 'Ada' }, { type: 'SUBMIT_PAYMENT', card: '4242' }, { type: 'PLACE_ORDER', outcome: 'approved' } ] as const; const transitionId = (source: string, event: string) => JSON.stringify(['transition', source, event, 0]); const branchId = (source: string, event: string, targets: string[]) => JSON.stringify(['transitionTarget', source, event, 0, targets]); async function runHappyPath() { const { coverage } = await testPaths(checkoutMachine, { fromEvents: happyPath as unknown as { type: string }[] }); return coverage; } describe('function transition coverage', () => { it('records the branch each function transition took', async () => { const coverage = await runHappyPath(); expect(coverage.transitionTargets.covered).toEqual( [ branchId('checkout.cart', 'CHECKOUT', ['checkout.shipping']), branchId('checkout.shipping', 'SUBMIT_SHIPPING', ['checkout.payment']), branchId('checkout.payment', 'SUBMIT_PAYMENT', ['checkout.review']), branchId('checkout.review', 'PLACE_ORDER', ['checkout.confirmed']) ].sort() ); expect(coverage.transitionTargets.covered).not.toContain( branchId('checkout.shipping', 'SUBMIT_SHIPPING', [ 'checkout.shipping.invalid' ]) ); }); it('distinguishes branches of one function transition', async () => { const { coverage } = await testPaths(checkoutMachine, { fromEvents: [ { type: 'CHECKOUT' }, { type: 'SUBMIT_SHIPPING' }, { type: 'SUBMIT_SHIPPING', name: 'Ada' }, { type: 'SUBMIT_PAYMENT', card: '4242' }, { type: 'PLACE_ORDER', outcome: 'declined' }, { type: 'SUBMIT_PAYMENT', card: '4242' }, { type: 'PLACE_ORDER', outcome: 'approved' } ] as { type: string }[] }); expect( coverage.transitions.counts[ transitionId('checkout.shipping', 'SUBMIT_SHIPPING') ] ).toBe(2); expect(coverage.transitionTargets.counts).toMatchObject({ [branchId('checkout.shipping', 'SUBMIT_SHIPPING', [ 'checkout.shipping.invalid' ])]: 1, [branchId('checkout.shipping', 'SUBMIT_SHIPPING', [ 'checkout.payment' ])]: 1, [branchId('checkout.review', 'PLACE_ORDER', [ 'checkout.payment.declined' ])]: 1, [branchId('checkout.review', 'PLACE_ORDER', ['checkout.confirmed'])]: 1 }); }); it('records branches in executed mode', async () => { const { coverage } = await testPaths(checkoutMachine, { mode: 'executed', fromEvents: [{ type: 'CHECKOUT' }, { type: 'SUBMIT_SHIPPING' }] as { type: string; }[] }); expect(coverage.transitionTargets.covered).toContain( branchId('checkout.shipping', 'SUBMIT_SHIPPING', [ 'checkout.shipping.invalid' ]) ); }); it('declares static transitions with their config targets', async () => { const coverage = await runHappyPath(); expect(coverage.transitionTargets.uncovered).toContain( branchId('checkout.confirmed', 'START_OVER', ['checkout.cart']) ); // Function transition branches are only known once observed. expect( [ ...coverage.transitionTargets.uncovered, ...coverage.transitionTargets.unknown, ...coverage.transitionTargets.unreachable ].some((id) => id.includes('"PLACE_ORDER"')) ).toBe(false); }); it('reports targetless transitions with an empty target list', async () => { const machine = createMachine({ id: 'm', initial: 'a', states: { a: { on: { PING: () => ({}), GO: { target: 'b' } } }, b: {} } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'PING' }, { type: 'GO' }] }); expect(coverage.transitionTargets.covered).toEqual([ JSON.stringify(['transitionTarget', 'm.a', 'GO', 0, ['m.b']]), JSON.stringify(['transitionTarget', 'm.a', 'PING', 0, []]) ]); }); it('includes the dimension in reports and thresholds', async () => { const coverage = await runHappyPath(); expect(testCoverageToJSON(coverage).dimensions.transitionTargets).toEqual( expect.objectContaining({ coveredIds: coverage.transitionTargets.covered }) ); expect(formatTestCoverage(coverage)).toContain('transitionTargets: '); expect(() => assertTestCoverage(coverage, { transitionTargets: 1 }) ).toThrow(/transitionTargets/); }); it('treats transitions out of visited states as uncovered, not unknown', async () => { const coverage = await runHappyPath(); for (const id of [ transitionId('checkout.payment', 'BACK'), transitionId('checkout.review', 'BACK'), transitionId('checkout.confirmed', 'START_OVER') ]) { expect(coverage.transitions.unknown).not.toContain(id); expect(coverage.transitions.uncovered).toContain(id); } expect(coverage.transitions.unknown).toEqual([]); expect(coverage.eventTypes.unknown).toEqual([]); expect(coverage.eventTypes.uncovered).toContain('START_OVER'); }); it('keeps unvisited parts that only function transitions reach unknown', async () => { const { coverage } = await testPaths(checkoutMachine, { fromEvents: [{ type: 'CHECKOUT' }] }); // `payment` is reachable only through `SUBMIT_SHIPPING`, which never ran. expect(coverage.stateNodes.unknown).toContain('checkout.payment'); expect(coverage.transitions.unknown).toContain( transitionId('checkout.payment', 'BACK') ); expect(coverage.transitions.uncovered).toContain( transitionId('checkout.shipping', 'BACK') ); }); it('keeps states that only unvisited branches target unknown', async () => { const coverage = await runHappyPath(); // `cart.empty` has no incoming transition, so a function transition // could still reach it. expect(coverage.stateNodes.unknown).toEqual([ 'checkout.cart.empty', 'checkout.payment.declined', 'checkout.payment.invalid', 'checkout.shipping.invalid' ]); expect(coverage.stateNodes.uncovered).toEqual([]); }); it('treats states statically reachable from visited states as uncovered', async () => { const machine = createMachine({ id: 'm', initial: 'a', states: { a: { on: { GO: () => ({ target: 'b' }) } }, b: { on: { NEXT: { target: 'c' } } }, c: { on: { DONE: { target: 'd' } } }, d: {}, island: {} } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'GO' }] }); expect(coverage.stateNodes.uncovered).toEqual(['m.c', 'm.d']); expect(coverage.stateNodes.unknown).toEqual(['m.island']); expect(coverage.transitions.uncovered).toEqual([ JSON.stringify(['transition', 'm.b', 'NEXT', 0]), JSON.stringify(['transition', 'm.c', 'DONE', 0]) ]); expect(coverage.transitions.unknown).toEqual([]); }); it('follows states a function transition enters transiently', async () => { const machine = createMachine({ id: 'm', initial: 'a', states: { a: { on: { GO: () => ({ target: 'routing' }) } }, routing: { always: () => ({ target: 'approved' }), on: { CANCEL: { target: 'declined' } } }, approved: {}, declined: {} } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'GO' }] }); expect(coverage.transitions.uncovered).toEqual([ JSON.stringify(['transition', 'm.routing', 'CANCEL', 0]) ]); expect(coverage.transitionTargets.uncovered).toEqual([ JSON.stringify([ 'transitionTarget', 'm.routing', 'CANCEL', 0, ['m.declined'] ]) ]); // State node coverage counts settled snapshots, so the transient // `routing` state is reachable but not covered. expect(coverage.stateNodes.uncovered).toEqual(['m.declined', 'm.routing']); }); it('activates the ancestors of nested targets', async () => { const machine = createMachine({ id: 'm', initial: 'a', states: { a: { on: { GO: () => ({ target: 'b' }) } }, b: { on: { NEXT: { target: 'group.child' } } }, group: { initial: 'first', on: { RESET: { target: 'a' } }, states: { first: {}, child: {} } } } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'GO' }] }); expect(coverage.transitions.uncovered).toContain( JSON.stringify(['transition', 'm.group', 'RESET', 0]) ); expect(coverage.stateNodes.uncovered).toEqual(['m.group', 'm.group.child']); // An explicit child target does not enter the parent's initial state. expect(coverage.stateNodes.unknown).toEqual(['m.group.first']); }); it('treats parts reachable from a start snapshot as uncovered', async () => { const machine = createMachine({ id: 'm', initial: 'idle', states: { idle: {}, review: { on: { SUBMIT: { target: 'done' } } }, done: {} } }); const [snapshot] = initialTransition(machine); const { coverage } = await propertyTest(machine, { adapter: fastCheckAdapter({ seed: 1, numRuns: 2, maxCommands: 1 }), start: { snapshot: machine.resolveState({ value: 'review' }) as typeof snapshot, serializeSnapshot: (value) => value.toJSON() }, events: { PING: fc.constant({}) }, invariant: () => {} }); expect(coverage.transitions.uncovered).toEqual([ JSON.stringify(['transition', 'm.review', 'SUBMIT', 0]) ]); expect(coverage.stateNodes.uncovered).toEqual(['m.done', 'm.idle']); expect(coverage.eventTypes.uncovered).toEqual(['SUBMIT']); }); it('does not count wildcard descriptors as uncovered event types', async () => { const machine = createMachine({ id: 'm', initial: 'a', states: { a: { on: { GO: () => ({ target: 'b' }) } }, b: { on: { 'item.*': {} } } } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'GO' }, { type: 'item.added' }] }); expect(coverage.transitions.uncovered).toEqual([]); expect(coverage.eventTypes.uncovered).toEqual([]); expect(() => assertTestCoverage(coverage, { eventTypes: 1 })).not.toThrow(); }); it('counts exact event types that contain an asterisk', async () => { const machine = createMachine({ id: 'm', initial: 'a', states: { a: { on: { GO: () => ({ target: 'b' }) } }, b: { on: { 'build*ready': {} } } } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'GO' }] }); expect(coverage.eventTypes.uncovered).toEqual(['build*ready']); }); it('does not enter the defaults of regions a transition targets explicitly', async () => { const machine = createMachine({ id: 'm', initial: 'outside', states: { outside: { on: { GO: { target: ['#m.p.r1.b', '#m.p.r2.d'] } } }, p: { type: 'parallel', states: { r1: { initial: 'a', states: { a: {}, b: {} } }, r2: { initial: 'c', states: { c: {}, d: {} } } } } } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'GO' }] }); expect(coverage.stateNodes.unreachable).toEqual(['m.p.r1.a', 'm.p.r2.c']); expect(coverage.stateNodes.uncovered).toEqual([]); }); it('does not treat history nodes as parallel regions', async () => { const machine = createMachine({ id: 'm', initial: 'outside', states: { outside: { on: { GO: { target: ['#m.p.r1.b', '#m.p.r2.d'] } } }, p: { type: 'parallel', states: { r1: { initial: 'a', states: { a: {}, b: {} } }, r2: { initial: 'c', states: { c: {}, d: {} } }, hist: { type: 'history', target: 'r1' } } } } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'GO' }] }); expect(coverage.stateNodes.unreachable).toEqual([ 'm.p.hist', 'm.p.r1.a', 'm.p.r2.c' ]); }); it('keeps default entry inside the entered node', async () => { const machine = createMachine({ id: 'm', initial: 'outside', states: { outside: { on: { GO: { target: ['#m.p.r1.b', '#m.p.r2.d'] } } }, p: { type: 'parallel', states: { r1: { initial: 'a', states: { a: {}, b: { initial: 'leaf', states: { leaf: {} } } } }, r2: { initial: 'c', states: { c: {}, d: {} } } } } } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'GO' }] }); expect(coverage.stateNodes.unreachable).toEqual(['m.p.r1.a', 'm.p.r2.c']); }); it('follows executed target sets that differ from static ones', async () => { const machine = createMachine({ id: 'm', initial: 'outside', states: { outside: { on: { STATIC: { target: ['#m.p.r1.b', '#m.p.r2.d'] }, FN: () => ({ target: '#m.p.r1.b' }) } }, p: { type: 'parallel', always: { target: 'after' }, states: { r1: { initial: 'a', states: { a: {}, b: {} } }, r2: { initial: 'c', states: { c: {}, d: {} } } } }, after: {} } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'FN' }] }); // `FN` enters `r2` by default, so `c` is reachable even though no settled // snapshot shows it. expect(coverage.stateNodes.unknown).not.toContain('m.p.r2.c'); expect(coverage.stateNodes.uncovered).toContain('m.p.r2.c'); }); it('narrows a non-reentering domain through nested parallel regions', async () => { const machine = createMachine({ id: 'm', initial: 'outside', states: { outside: { on: { GO: { target: ['#m.p.q.r1.b', '#m.p.q.r2.d'] } } }, p: { type: 'parallel', on: { NEXT: { target: '#m.p.q.r1.b' } }, states: { q: { type: 'parallel', states: { r1: { initial: 'a', states: { a: {}, b: {} } }, r2: { initial: 'c', states: { c: {}, d: {} } } } }, s: { initial: 'x', states: { x: {} } } } } } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'GO' }] }); expect(coverage.stateNodes.unreachable).toEqual([ 'm.p.q.r1.a', 'm.p.q.r2.c' ]); }); it('enters sibling regions when a parallel source is reentered', async () => { const machine = createMachine({ id: 'm', initial: 'outside', states: { outside: { on: { GO: { target: ['#m.p.r1.b', '#m.p.r2.d'] } } }, p: { type: 'parallel', on: { RESET: { target: '#m.p.r1.b', reenter: true } }, states: { r1: { initial: 'a', states: { a: {}, b: {} } }, r2: { initial: 'c', states: { c: {}, d: {} } } } } } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'GO' }] }); expect(coverage.stateNodes.unreachable).toEqual(['m.p.r1.a']); expect(coverage.stateNodes.uncovered).toEqual(['m.p.r2.c']); }); it('uses history default targets to decide region default entry', async () => { const machine = createMachine({ id: 'm', initial: 'outside', states: { outside: { on: { GO: { target: ['#m.p.hist', '#m.p.r1.b'] } } }, p: { type: 'parallel', states: { r1: { initial: 'a', states: { a: {}, b: {} } }, r2: { initial: 'c', states: { c: {}, d: {} } }, hist: { type: 'history', target: 'r2.d' } } } } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'GO' }] }); expect(coverage.stateNodes.unreachable).toEqual(['m.p.r1.a', 'm.p.r2.c']); }); it('does not replay a history default for a restored history', async () => { const machine = createMachine({ id: 'm', initial: 'outside', states: { outside: { on: { GO: { target: '#m.p.b' }, RETURN: () => ({ target: '#m.p.hist' }) } }, p: { initial: 'b', on: { LEAVE: { target: '#m.outside' } }, states: { a: { on: { NEXT: { target: 'b' } } }, b: {}, hist: { type: 'history', target: 'a' } } } } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'GO' }, { type: 'LEAVE' }, { type: 'RETURN' }] }); expect(coverage.stateNodes.unknown).toContain('m.p.a'); expect(coverage.transitions.unknown).toContain( JSON.stringify(['transition', 'm.p.a', 'NEXT', 0]) ); }); it('enters the ancestors of deep initial targets', async () => { const machine = createMachine({ id: 'm', initial: 'a', states: { a: { on: { GO: () => ({ target: 'b' }) } }, b: { on: { NEXT: { target: 'group' } } }, group: { initial: '#deep', states: { nested: { on: { RESET: { target: '#m.a' } }, initial: 'leaf', states: { leaf: { id: 'deep' } } } } } } }); const { coverage } = await testPaths(machine, { fromEvents: [{ type: 'GO' }] }); expect(coverage.stateNodes.unknown).toEqual([]); expect(coverage.transitions.uncovered).toContain( JSON.stringify(['transition', 'm.group.nested', 'RESET', 0]) ); }); });