Registry / testing / spy-middleware

spy-middleware

JSON →
library1.2.2jsnpmunverified

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.

npm install spy-middleware
INSTALL
IMPORT
SIG · SPY-MIDDLEWARE
S
spy-middleware
testingjavascriptv1.2.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

makeSpyMiddleware
import makeSpyMiddleware from 'spy-middleware'
const makeSpyMiddleware = require('spy-middleware')
Package is ESM-only; CommonJS require yields module.exports which is not the default export directly. Use named import or default import as shown.
default export
import makeSpyMiddleware from 'spy-middleware'
import { makeSpyMiddleware } from 'spy-middleware'
The package exports a default function; named import will not work.
spy middleware instance methods
const spyMiddleware = makeSpyMiddleware(); spyMiddleware.getActions(); spyMiddleware.untilNext(); etc.
makeSpyMiddleware().getActions()
Instance methods are available on the returned middleware object after creation, but you must apply it via applyMiddleware to a store for it to record actions.

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));
Debug
Known issues
gotchaThe 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)
affects: >=1.0.0
gotchaThe 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.
affects: >=1.0.0
gotchauntilNext() 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.
affects: >=1.0.0
deprecatedNone documented. Package is stable with no known deprecations.
fix
N/A
affects: >=0.0.0
gotchaThe 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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: makeSpyMiddleware is not a function
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'
Actions not being recorded
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.
Promise never resolves when using untilNext
The matching action was already dispatched before untilNext was called.
fix
Use spyMiddleware.until() instead if you expect the action might have already fired.
spyMiddleware.untilNext is not a function
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().
Upgrade
Version history
1.2.2latest on npm
Audit
Dependencies
reduxrequiredPeer dependency required for middleware usage with createStore and applyMiddleware
Agent activity
2 hits · last 30 days
node
2
Resources
spy-middleware — npm install spy-middleware · libregistry