Skip to content

Testing — overview & philosophy

When state changes are data, most of what you’d test in a typical React app moves out of the component layer and into pure functions you can call directly. Component tests still exist — but they shrink, become less brittle, and stop being where the interesting logic gets verified.

This page covers the philosophy. The other two pages cover concrete recipes.

Imperative React — testing is mostly component testing

Section titled “Imperative React — testing is mostly component testing”
// What you're testing: behaviour buried in a closure.
function Counter({ onLog }: { onLog: (n: number) => void }) {
const [count, setCount] = useState(0);
const increment = () => {
const next = count + 1;
setCount(next);
onLog(next);
};
return <button onClick={increment}>{count}</button>;
}
// Test must mount, click, assert on rendered output AND on the mock call.
test('increments and logs', () => {
const onLog = vi.fn();
render(<Counter onLog={onLog} />);
fireEvent.click(screen.getByRole('button'));
expect(screen.getByRole('button')).toHaveTextContent('1');
expect(onLog).toHaveBeenCalledWith(1);
});

The logic — “increment and call onLog with the new value” — is invisible until the button is clicked. The test has to run the behaviour to inspect it. Refactoring the button into a wrapper component breaks the query selector. Renaming onLog is a search-and-replace across tests.

Declarative input-actions — most of the logic is pure functions

Section titled “Declarative input-actions — most of the logic is pure functions”
// What you're testing: pure derivations + pure action data.
import { useLocalActions, applyAction } from '@input-actions/core';
// 1. Pure derivation — test with no React.
function prepareCounterProps(state: { count: number }) {
return { label: `${state.count}`, disabled: state.count >= 10 };
}
test('prepareCounterProps caps at 10', () => {
expect(prepareCounterProps({ count: 9 })).toEqual({ label: '9', disabled: false });
expect(prepareCounterProps({ count: 10 })).toEqual({ label: '10', disabled: true });
});
// 2. Action data — assert the tuple directly.
test('increment dispatches the right action', () => {
expect(['action/increment', ['count']]).toEqual(['action/increment', ['count']]);
});
// 3. State transition — apply the action, check the state.
test('increment increments', () => {
const next = applyAction({ count: 0 }, ['action/increment', ['count']], { count: 0 });
expect(next).toEqual({ count: 1 });
});

Three tests, no rendering, no fake event firing, no screen queries. Each piece is tested in isolation. The component (if you write one) is now a visual binding — its test would just confirm that the right state is rendered and the right action is dispatched on click, but the logic of “what should happen” was already verified above.

LayerImperative Reactinput-actions
Pure-function unit testssmallmost of your tests
Reducer / validator testssometimes (Redux)direct & easy — applyAction, FSM validator
Component integration testsmost of your teststhin layer — “does the component dispatch when I click?”
End-to-end / snapshotbrittle, commonoptional

The interesting logic moves down the pyramid. Component tests don’t disappear — but they become the smallest layer, not the largest.

Pure selectors / prepareUIData

State → props functions. Call with a state literal, assert the output. No React in the test file.

Action data

Tuples are values. defineBindings(…) returns its input — you can inspect it. Factory functions are pure.

applyAction

The shared sync reducer. Call applyAction(state, action, initial) — assert the new state. No store required.

StateMachineValidator

The FSM validator is a class with pure methods. validator.validateTransition(name, state, from, to) returns the verdict + side-effect actions.

Async handlers

Just async functions. Pass mock data, mock the AbortSignal, await the result. No store needed for the handler itself.

Bindings entries

Either a static record (literal data — compare directly) or a factory (call with sample params, compare the tuple list it returns).

Three places where rendering is still useful

Section titled “Three places where rendering is still useful”
  1. Wiring smoke tests. “When I click this button, does some action get dispatched?” — verifies you didn’t forget to pass actions(...) to onClick.
  2. Subscription correctness. “When state changes, does this component re-render?” — verifies the useStore selector reads the right slice.
  3. Full integration tests. “Submitting the form transitions the FSM and the success step renders.” — chains async, FSM, and bindings together.

The component testing page covers each pattern.

Async strategies (prevent, cancel, queue, debounce, allow), machine actions (transition, transition-if, force, sequence, retry, delay), bindings (flat, factory, machine-rooted) — each is exercised by the library’s own test suite. Your app code doesn’t need to re-test the library; you test the handlers and machine definitions you supply.

See packages/core/src/**/*.test.{ts,tsx} for the library’s canonical patterns. 65+ test cases across the hook, the provider, the path utils, the bindings system, and the action API.

The library and its examples use:

  • vitest — Vite-native, fast, similar API to Jest.
  • @testing-library/react — for the small subset of tests that need to mount components.
  • act() + renderHook() — for hook-only tests that don’t need a full mount.

These are the choices in packages/core and across the example projects. The recipes on the next two pages assume the same setup; the patterns translate cleanly to Jest if you prefer.

The packages are ES modules only. Jest runs CommonJS by default, so it needs one of two setups. Both are tested on every change.

Transform the packages with Babel. This works on any Node version:

// babel.config.cjs (a project-wide file: a `babel` key in package.json
// doesn't apply to node_modules)
module.exports = {
presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
};
package.json
"jest": {
"testEnvironment": "jsdom",
"transform": { "\\.[cm]?js$": "babel-jest" },
"transformIgnorePatterns": ["/node_modules/(?!@input-actions/)"]
}

Or run Jest in ESM mode with node --experimental-vm-modules. This works on Node 22. On Node 24, Jest’s ESM mode fails with module is already linked for packages shaped like ours, a Jest bug that isn’t specific to this library. Use the Babel setup there.