Skip to content

Tests

kotick turns off only when process.env.NODE_ENV is "production". Test runners such as Vitest and Jest set it to "test". Thus, in your tests, kotick works as it does in development.

What kotick does in a test

What kotick does depends on the test environment:

EnvironmentWhat kotick does
A DOM, such as jsdomuseMachineReducer, the hooks of kotick/xstate and connect() register each instance in a registry on globalThis. kotick records the context of each instance in sessionStorage, under keys that start with kotick:recorded:. If the test renders <Kotick />, kotick adds a <div data-kotick-root> to document.body.
node, with no DOMNothing. kotick does nothing when window or document is missing, as on the server.

Without a preview, useMachineReducer behaves as it does in production, and the hooks of kotick/xstate behave as the hooks of @xstate/react. Thus, your tests do not have to change. kotick writes nothing to the console when it has no problem.

A test that reads sessionStorage also sees the keys of kotick. kotick writes them 500 ms after a transition, so a write can occur during the next test.

Turn kotick off in tests

Mock kotick in a setup file, so that <Kotick /> renders nothing. This example is for Vitest:

Add the file to test.setupFiles in the Vitest config. Other test runners have a function that does the same thing, for example jest.mock() in Jest.

The next step depends on the adapter:

Do not mock kotick/reducer. Its hook runs your transitions and effects, so a mock must keep them. Without a preview, the hook gives the state and the effects of its production version, because it calls that version. It also registers each instance and writes kotick:recorded: keys, as the XState hooks do.

Test a reducer machine

machine.reducer is the plain reducer of a reducer machine, so a test of its transitions needs no React. For the search machine, searchMachine.reducer({ status: 'idle' }, { type: 'SEARCH', query: 'linen' }) returns { status: 'loading', query: 'linen' }. A state that does not list an event returns the same object, so toBe(state) checks that the state ignores the event.