Gathering detailed insights and metrics for redux-mock-store-await-actions
Gathering detailed insights and metrics for redux-mock-store-await-actions
Gathering detailed insights and metrics for redux-mock-store-await-actions
Gathering detailed insights and metrics for redux-mock-store-await-actions
Waits for specific actions to be dispatched or a timeout expires
npm install redux-mock-store-await-actions
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
7 Stars
50 Commits
1 Forks
9 Watchers
1 Branches
4 Contributors
Updated on Jan 28, 2023
Latest Version
2.1.0
Package Id
redux-mock-store-await-actions@2.1.0
Unpacked Size
32.28 kB
Size
7.86 kB
File Count
14
NPM Version
5.6.0
Node Version
8.9.4
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Waits for specific actions to be dispatched or a timeout expires.
NOTE: This module only works with redux-mock-store and shall only be used for testing purposes. Support for real Redux store is not provided.
$ npm install redux-mock-store-await-actions --save-dev
Consider the following example:
1function login(username, password) { 2 return async (dispatch) => { 3 dispatch({ type: 'LOGIN_START', payload: { username, password } }); 4 5 try { 6 const user = await fetch('/login', { 7 headers: { 'Content-Type': 'application/json' }, 8 method: 'POST', 9 body: JSON.stringify({ username, password }) 10 }); 11 12 dispatch({ type: 'LOGIN_SUCCESS', payload: user }); 13 } catch (err) { 14 dispatch({ type: 'LOGIN_FAIL', payload: err }); 15 throw err; 16 } 17 18 // Fetch orders asynchronously, not waiting for them to be retrieved 19 dispatch(fetchOrders()); 20 } 21} 22 23function fetchOrders() { 24 return async (dispatch) => { 25 dispatch({ type: 'FETCH_ORDERS_START' }); 26 27 try { 28 const orders = await fetch('/account/orders'); 29 30 dispatch({ type: 'FETCH_ORDERS_SUCCESS', payload: orders }); 31 } catch (err) { 32 dispatch({ type: 'FETCH_ORDERS_FAIL', payload: err }); 33 throw err; 34 } 35 } 36} 37 38store.dispatch(login('my-username', 'my-password')); 39 40expect(store.getActions()).toContain([ 41 'LOGIN_START', 42 'FETCH_ORDERS_SUCCESS' 43]);
The assertion above will fail because FETCH_ORDERS_SUCCESS
will not yet exist in the stack of actions.
To solve this, one can use setTimeout
explicitly in each test:
1store.dispatch(login('my-username', 'my-password')); 2 3setTimeout(() => expect(store.getActions()).toContain([ 4 'LOGIN_START', 5 'FETCH_ORDERS_SUCCESS' 6]), 50);
However, this is not pretty and is error-prone. redux-mock-store-await-actions
makes this easier for you.
Supply the action types to await for.
1import waitForActions from 'redux-mock-store-await-actions'; 2import configureStore from 'redux-mock-store'; 3import thunkMiddleware from 'redux-thunk'; 4 5const store = configureStore([thunkMiddleware])(); 6 7store.dispatch(login('my-username', 'my-password')); 8 9await waitForActions(store, ['LOGIN_START', 'FETCH_ORDERS_SUCCESS']);
Supply the action objects to await for, matching a subset of the properties of the dispatched actions. It performs a deep comparison between property values of dispatched and expected actions to determine whether the expected actions are partially contained in the stack of dispatched actions.
1import waitForActions from 'redux-mock-store-await-actions'; 2import configureStore from 'redux-mock-store'; 3import thunkMiddleware from 'redux-thunk'; 4 5const store = configureStore([thunkMiddleware])(); 6 7store.dispatch(login('my-username', 'my-password')); 8// { type: 'LOGIN_START', payload: { username: 'my-username' } } 9// matches 10// { type: 'LOGIN_START', payload: { username: 'my-username', password } } 11// 12// { type: 'FETCH_ORDERS_SUCCESS', } 13// matches 14// { type: 'FETCH_ORDERS_SUCCESS', payload: orders } 15 16await waitForActions(store, [ 17 { 18 type: 'LOGIN_START', 19 payload: { username: 'my-username' }, 20 }, 21 { 22 type: 'FETCH_ORDERS_SUCCESS', 23 }, 24 25]);
Returns a Promise
which fulfills if all actions
are dispatched before the timeout expires. The Promise
has a .cancel()
function which, if called, will reject the Promise
.
The Promise
might be rejected:
TimeoutError
.cancel()
invocation, throwing CancelledError
MismatchError
NOTE: Subsequent calls to waitForActions
with the same actions should be preceded by a call to store.clearActions()
, otherwise the returned Promise
will resolve immediately.
Type: Object
The redux-mock-store
.
Type: Object
String
Array
Function
The actions to wait for. It can be either:
String
: an action type string.Object
: an action object.Array
of either
Type: Number
Default: 2000
The timeout given in milliseconds.
Type: Number
Default: 0
Specifies the time in milliseconds that every invocation to the action's matcher take place at since the last invocation. When set to zero, throttling is disabled.
When throttling is enabled, the matcher
will be called at most once per throttleWait
milliseconds receiving the array of actions dispatched until that time. If the matcher
does not resolve the Promise
until timeout
milliseconds have elapsed, the Promise
is rejected throwing TimeoutError
.
This feature is useful when one needs to wait for several actions or a burst of actions to be dispatched, effectively skip invocations to the action's matcher until the Redux store "settles" to avoid running complex action comparison logic in the meantime and improve performance.
Type: Function
Default: .matchers.order
Supplies custom behavior to specify how expected and dispatched actions should be compared. The function accepts two arguments: the array of expected actions and dispatched actions.
The matcher must either:
true
to indicate a match has occurred and fulfill the Promise
false
to indicate a match is yet to occur and the Promise
remains in pending stateMismatchError
to indicate a match will not occur anymore and reject the Promise
Two built-in matchers are already shipped and available under .matchers
property:
order
matcher performs a comparison between the specified order of expected actions against the order of arrival of dispatched actions. On the first mismatch detected, MismatchError
is thrown for early rejectioncontaining
matcher is a less strict matcher which checks whether expected actions are contained within dispatched actionsBoth matchers perform a partial deep comparison between dispatched and expected actions, as per Lodash's isMatch().
Example of a custom matcher implementation:
1import waitForActions from 'redux-mock-store-await-actions'; 2import configureStore from 'redux-mock-store'; 3import thunkMiddleware from 'redux-thunk'; 4 5const store = configureStore([thunkMiddleware])(); 6const expectedActions = [ 7 { type: 'LOGIN_START', payload: { username: 'my-username' } }, 8 { type: 'FETCH_ORDERS_SUCCESS' } 9]; 10 11store.dispatch(login('my-username', 'my-password')); 12// Throws if LOGIN_FAIL is dispatched or 13// Matches when LOGIN_START and FETCH_ORDERS_SUCCESS are dispatched 14 15waitForActions(store, expectedActions, { matcher: (expectedActions, storeActions) => { 16 const hasLoginFail = storeActions.some((action) => action.type === 'LOGIN_FAIL'); 17 18 if (hasLoginFail) { 19 throw new waitForActions.MismatchError(); 20 } 21 22 const hasLoginStart = storeActions.some((action) => action.type === 'LOGIN_START' && action.payload.username === 'my-username'); 23 const hasFetchOrdersSuccess = storeActions.some((action) => action.type === 'FETCH_ORDERS_SUCCESS'); 24 25 return hasLoginStart && hasFetchOrdersSuccess; 26}}) 27.then(() => { 28 // Expected actions dispatched 29}) 30.catch((err) => { 31 // MismatchError 32});
$ npm test
$ npm test -- --watch
during development
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 4/16 approved changesets -- score normalized to 2
Reason
project is archived
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
51 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More