Registry / testing / redux-waitfor-middleware

redux-waitfor-middleware

JSON →
library1.0.1jsnpmunverified

Redux Waitfor Middleware (current stable version 1.0.1) is a lightweight utility designed to facilitate testing of Redux applications, particularly for asynchronous flows. It operates as a standard Redux middleware, recording dispatched actions and providing a `waitFor` method that allows tests to pause execution until a specific set of actions has been dispatched within a given timeout. The library is primarily used in testing environments to assert on the sequence and payload of actions after asynchronous operations complete, such as API calls. Its release cadence appears to be stable, with the current version being quite mature. A key differentiator is its simplicity and direct focus on action-based waiting, making it less verbose than complex mocking or promise-chaining in some test scenarios. It also handles the case where actions are already dispatched, immediately resolving the wait.

npm install redux-waitfor-middleware
INSTALL
IMPORT
SIG · REDUX-WAITFOR-MIDD
R
redux-waitfor-middleware
testingjavascriptv1.0.1
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

createWaitForMiddleware
✓ import createWaitForMiddleware from 'redux-waitfor-middleware';
✗ const createWaitForMiddleware = require('redux-waitfor-middleware');
The library primarily uses a default export for the middleware factory. While CommonJS `require` might work in some transpiled environments, native ESM import is preferred.
waitFor
✓ const waitForMiddleware = createWaitForMiddleware(); await waitForMiddleware.waitFor(['ACTION_TYPE']);
This is a method on the instance returned by `createWaitForMiddleware`, not a direct import.
clean
✓ waitForMiddleware.clean();
This is a method on the instance returned by `createWaitForMiddleware`, essential for resetting recorded actions between tests.

This quickstart demonstrates how to integrate `redux-waitfor-middleware` into a Redux store, simulate an asynchronous action flow, and then use `waitForMiddleware.waitFor()` to assert that a specific action ('FETCH_DATA_SUCCESS') is dispatched within a given timeout.

import createWaitForMiddleware from 'redux-waitfor-middleware'; import { createStore, applyMiddleware } from 'redux'; // A dummy reducer for demonstration const exampleReducer = (state = { data: [] }, action) => { switch (action.type) { case 'FETCH_DATA_REQUEST': return { ...state, loading: true }; case 'FETCH_DATA_SUCCESS': return { ...state, loading: false, data: action.payload }; case 'FETCH_DATA_FAILURE': return { ...state, loading: false, error: action.error }; default: return state; } }; const waitForMiddleware = createWaitForMiddleware(); const store = createStore(exampleReducer, applyMiddleware(waitForMiddleware)); async function simulateFetch() { store.dispatch({ type: 'FETCH_DATA_REQUEST' }); console.log('Dispatched FETCH_DATA_REQUEST'); // Simulate an async operation setTimeout(() => { store.dispatch({ type: 'FETCH_DATA_SUCCESS', payload: ['item1', 'item2'] }); console.log('Dispatched FETCH_DATA_SUCCESS'); }, 100); } async function runTest() { console.log('Running test...'); simulateFetch(); try { const successActions = await waitForMiddleware.waitFor(['FETCH_DATA_SUCCESS'], 500); console.log('Test passed! Received actions:', successActions); console.assert(successActions.length === 1, 'Expected one success action'); console.assert(successActions[0].payload.includes('item1'), 'Payload check'); } catch (error) { console.error('Test failed:', error.message); } finally { // Always clean up after a test waitForMiddleware.clean(); console.log('Cleaned recorded actions.'); } } runTest();
Debug
Known issues
gotchaActions dispatched before the `waitFor` call but after the last `clean()` call will immediately resolve the `waitFor` promise. This can lead to flaky tests if `clean()` is not called consistently between test cases.
fix
Always call `waitForMiddleware.clean()` at the beginning or end of each individual test case (e.g., in `beforeEach` or `afterEach` hooks) to ensure a fresh state for each test.
affects: >=1.0.0
breakingThe `waitFor` method throws an `Error` if the specified actions are not dispatched within the provided timeout. This will fail the test by default and needs to be handled if non-receipt of an action is an expected test outcome.
fix
Wrap `await waitForMiddleware.waitFor(...)` calls in a `try...catch` block if you need to specifically handle timeout scenarios without failing the test, though typically, a timeout indicates a test failure.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Timeout of XXXXms reached waiting for actions: ACTION_TYPE_A, ACTION_TYPE_B
One or more of the specified action types were not dispatched within the allocated timeout period.
fix
Verify that the actions are indeed dispatched by your application logic, increase the timeout value if the operation genuinely takes longer, or ensure that `clean()` isn't prematurely clearing an action you're waiting for from a prior test.
TypeError: Cannot read properties of undefined (reading 'waitFor')
The `waitForMiddleware` instance was not correctly created or is out of scope. The `createWaitForMiddleware` function returns an object with `waitFor` and `clean` methods.
fix
Ensure `const waitForMiddleware = createWaitForMiddleware();` is called and `waitForMiddleware` is accessible where you are attempting to use its methods. It's often passed around or made available via a setup function in testing frameworks.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
reduxrequiredCore Redux library required for middleware integration and store creation.
Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
redux-waitfor-middleware — npm install redux-waitfor-middleware · libregistry