Skip to content

TestIntent inspects an intent descriptor’s configuration: which commands it references and what execution mode it uses. It answers the question “when this intent is dispatched, what will the store execute?”

import { TestIntent } from '@hurum/core/testing'
import { PurchaseIntents } from './intents'
const intent = TestIntent(PurchaseIntents.submitClicked)

TestIntent takes an intent descriptor (from your Intents() definition) and returns a read-only object with commands and mode.

The list of commands the intent will execute:

const intent = TestIntent(PurchaseIntents.submitClicked)
expect(intent.commands).toEqual([ValidateCommand, SavePurchaseCommand])

This verifies that the intent is wired to the correct commands. If you refactor an intent to add or remove a command, this test catches the change.

The execution mode — how the commands are run:

// Sequential (default): commands run one after another
const sequential = TestIntent(PurchaseIntents.submitClicked)
expect(sequential.mode).toBe('sequential')
// All (fail-fast parallel): commands run concurrently, abort all on first failure
const failFast = TestIntent(PurchaseIntents.loadAll)
expect(failFast.mode).toBe('all')
// AllSettled (independent parallel): commands run concurrently, all complete independently
const independent = TestIntent(PurchaseIntents.initialize)
expect(independent.mode).toBe('allSettled')
ModeCreated withBehavior
sequentialIntent(cmd1, cmd2)Run commands one after another
allIntent.all(cmd1, cmd2)Run concurrently, fail-fast on error
allSettledIntent.allSettled(cmd1, cmd2)Run concurrently, all complete independently
intents.test.ts
import { describe, it, expect } from 'vitest'
import { TestIntent } from '@hurum/core/testing'
import { PurchaseIntents } from './intents'
import {
ValidateCommand,
SavePurchaseCommand,
LoadPricesCommand,
LoadInventoryCommand,
} from './executors'
describe('PurchaseIntents', () => {
it('submitClicked runs validation then save', () => {
const intent = TestIntent(PurchaseIntents.submitClicked)
expect(intent.commands).toEqual([ValidateCommand, SavePurchaseCommand])
expect(intent.mode).toBe('sequential')
})
it('initialize loads prices and inventory concurrently', () => {
const intent = TestIntent(PurchaseIntents.initialize)
expect(intent.commands).toEqual([LoadPricesCommand, LoadInventoryCommand])
expect(intent.mode).toBe('allSettled')
})
it('plusClicked runs a single command', () => {
const intent = TestIntent(CounterIntents.plusClicked)
expect(intent.commands).toHaveLength(1)
expect(intent.commands).toEqual([IncrementCommand])
expect(intent.mode).toBe('sequential')
})
})

TestIntent is a lightweight sanity check. Use it when:

  • An intent wires multiple commands. Verify the commands are correct and in the right order.
  • An intent uses parallel execution. Confirm the mode is all or allSettled, not accidentally sequential.
  • You refactor command structure. A TestIntent test breaks immediately if a command is added, removed, or reordered.

For single-command intents with sequential mode, the test is simple but still valuable as documentation of intent:

expect(intent.commands).toEqual([IncrementCommand])
expect(intent.mode).toBe('sequential')