Skip to content

Fixtures

Fixtures give context to states that you have not recorded yet, such as an error after a slow request.

Define fixtures

Put them in a file of their own, and give <Kotick /> a loader for that file. Each adapter exports its own defineFixtures:

defineFixtures from kotick/reducer takes a state object for each state that you list. A fixture is a whole state of its own name, so its discriminant is the name of its key:

searchMachine is the machine of From a switch reducer.

The loader has a development condition. The condition removes the fixtures, and the import, from production builds. Use the condition of your bundler.

In Vite:

In Next.js and other bundlers:

Do not use process.env.NODE_ENV in Vite. A default Vite app does not have the Node.js types, thus TypeScript does not know process. In Next.js, put 'use client' at the top of the file. Refer to Next.js.

The examples import from src through the @/ alias. Next.js sets this alias, but Vite does not. In Vite, add the alias to paths in your TypeScript config and to resolve.alias in vite.config.ts, or use relative imports.

How fixtures apply

These rules apply to both adapters:

  • A fixture set applies to each instance of its machine.
  • The loader runs after the page loads.
  • You can give an array of sets in place of a loader. Then the sets are in your main bundle, also in production. Use a loader with a development check to keep them out.
  • kotick/reducer and kotick/xstate both export defineFixtures. A file with sets for both adapters imports one of them with another name, for example import { defineFixtures as defineReducerFixtures } from 'kotick/reducer'.

Each adapter adds rules of its own:

  • The keys are optional, so a set can start with the one state that the app cannot get to. satisfies CompleteFixtures<typeof searchMachine> makes the file fail when the machine has a state without a fixture.
  • TypeScript checks each fixture against the state of its key. Without TypeScript, defineFixtures leaves out a fixture of another state and a key that is not a state. In development, it logs a warning for each.
  • Copy context in the panel copies the state object of the shown state as JSON. The JSON is a fixture as it is.
  • Until the loader finishes, a state without a recording stays greyed out in the panel. Its row enables when the set arrives.