spy-middleware

raw JSON →
1.2.2 verified Sat Apr 25 auth: no javascript

spy-middleware is a Redux middleware for spying on dispatched actions in tests. Version 1.2.2 is the latest stable release. It provides methods to retrieve dispatched actions, query for specific actions, and await actions that have been dispatched or will be dispatched. Unlike simple action loggers, it supports promise-based waiting for actions (until/untilNext), which integrates well with async test patterns. Ideal for integration testing of Redux stores where you need to assert on action sequences or wait for asynchronous side effects.

error TypeError: makeSpyMiddleware is not a function
cause Wrong import: used named import {} instead of default import, or used require which gives the module object.
fix
Use default import: import makeSpyMiddleware from 'spy-middleware'
error Actions not being recorded
cause Spy middleware not applied to store, or applied incorrectly (e.g., not last in chain).
fix
Ensure spyMiddleware is passed to applyMiddleware and that the store is created with that enhancer.
error Promise never resolves when using untilNext
cause The matching action was already dispatched before untilNext was called.
fix
Use spyMiddleware.until() instead if you expect the action might have already fired.
error spyMiddleware.untilNext is not a function
cause Using an old version or incorrect object; ensure you are calling untilNext on the result of makeSpyMiddleware().
fix
Confirm you have version 1.0.0+ and that you created the middleware: const sm = makeSpyMiddleware(); then use sm.untilNext().
gotcha The spy middleware must be placed last in the middleware chain to capture all actions after other middleware process them.
fix Apply spyMiddleware as the last argument to applyMiddleware: applyMiddleware(otherMiddleware, spyMiddleware)
gotcha The getActions() method returns a new array each time, not a live reference; mutations to the returned array do not affect the recorded actions.
fix Assign the result to a variable if you need to manipulate it.
gotcha untilNext() resolves only if the matching action is dispatched after the call; if the action already happened, it never resolves. Use until() for matching past actions.
fix Use spyMiddleware.until() if you want to match actions that may have already been dispatched.
deprecated None documented. Package is stable with no known deprecations.
fix N/A
gotcha The condition parameter for getAction and until/untilNext can be a string, RegExp, or function, but undefined return from function does not match.
fix Ensure filter functions return a boolean or truthy value.
npm install spy-middleware
yarn add spy-middleware
pnpm add spy-middleware

Shows how to create spy middleware, apply to store, dispatch actions, get all actions, and wait for an action using untilNext.

import { createStore, applyMiddleware } from 'redux';
import makeSpyMiddleware from 'spy-middleware';

const spyMiddleware = makeSpyMiddleware();
const reducer = (state = [], action) => [...state, action.type];
const store = createStore(reducer, applyMiddleware(spyMiddleware));

store.dispatch({ type: 'FIRST' });
store.dispatch({ type: 'SECOND' });

console.log(spyMiddleware.getActions()); // [{ type: 'FIRST' }, { type: 'SECOND' }]

// Wait for a specific action
setTimeout(() => store.dispatch({ type: 'THIRD' }), 10);
spyMiddleware.untilNext('THIRD').then(action => console.log('Got:', action));