Gathering detailed insights and metrics for @jedmao/redux-mock-store
Gathering detailed insights and metrics for @jedmao/redux-mock-store
Gathering detailed insights and metrics for @jedmao/redux-mock-store
Gathering detailed insights and metrics for @jedmao/redux-mock-store
A mock store for testing Redux async action creators and middleware.
npm install @jedmao/redux-mock-store
Typescript
Module System
Node Version
NPM Version
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
19 Stars
409 Commits
6 Forks
13 Branches
1 Contributors
Updated on Sep 18, 2024
Latest Version
3.0.5
Package Id
@jedmao/redux-mock-store@3.0.5
Unpacked Size
21.44 kB
Size
6.30 kB
File Count
10
NPM Version
6.14.2
Node Version
12.16.1
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
1
29
A mock store for testing Redux async action creators and middleware. The mock store will create an array of dispatched actions which serve as an action log for tests.
This is a TypeScript fork of redux-mock-store.
Please note that this library is designed to test the action-related, not reducer-related logic (i.e., it does not update the Redux store). If you want a complex test combining actions and reducers together, take a look at other libraries (e.g., redux-actions-assertions). Refer to issue redux-mock-store#71 for more details.
1npm install @jedmao/redux-mock-store --save-dev
Or
1yarn add @jedmao/redux-mock-store --dev
You will benefit from configuring a single mockStore
for all of your tests. A
common example would be to configure your store with a
redux-thunk
middleware.
1import { configureMockStore } from '@jedmao/redux-mock-store' 2import thunk from 'redux-thunk' 3 4const middlewares = [thunk] 5 6export default configureMockStore(middlewares)
Let's do the same thing in TypeScript and add an extra thunk argument.
1import { configureMockStore } from '@jedmao/redux-mock-store' 2import thunk, { ThunkDispatch } from 'redux-thunk' 3 4// internal dependencies 5import RootState from 'store/RootState' 6import RootActions from 'actions' 7 8const extraThunkArgument = { foo: 'bar' } 9const middlewares = [thunk.withExtraArgument(extraThunkArgument)] 10 11export default configureMockStore< 12 RootState, 13 RootActions, 14 ThunkDispatch<RootState, typeof extraThunkArgument, RootActions> 15>(middlewares)
The mock store saves all the dispatched actions inside the store instance. You
can get all the actions by calling store.getActions()
.
1import { mockStore } from 'utils/test' 2 3describe('todo actions', () => { 4 let store: ReturnType<typeof mockStore> 5 6 beforeEach(() => { 7 store = mockStore(/* initial state */) 8 }) 9 10 it('dispatches ADD_TODO', () => { 11 const action = { type: 'ADD_TODO' } 12 13 store.dispatch(action) 14 15 expect(store.getActions()[0]).toBe(action) 16 }) 17})
1it('asynchronously dispatches SUCCESS', async () => { 2 const store = mockStore(/* initial state */) 3 const success = { type: 'SUCCESS' } 4 5 await store.dispatch(async dispatch => { 6 dispatch(success) 7 }) 8 9 expect(store.getActions()[0]).toBe(success) 10})
See the tests for more thorough examples.
Configure the mock store by applying middlewares.
1configureMockStore< 2 S = any, 3 A extends Redux.Action = Redux.AnyAction, 4 DispatchExts extends {} | void = void 5>( 6 middlewares: Redux.Middleware[] = [], 7): MockStoreCreator<S, A, DispatchExts>
Calling configureMockStore
will return a
MockStoreCreator
, which returns an instance of the
configured mock store. This MockStoreCreator
is a
function named mockStore
.
Call this function to reset your store after every test.
1function mockStore( 2 getState: S | MockGetState<S> = {} as S, 3): DispatchExts extends void 4 ? MockStore<S, A> 5 : MockStoreEnhanced<S, A, DispatchExts>
Dispatches an action T
through the mock store. The action will be stored in an
array inside the instance and executed.
1dispatch<T extends A>(action: T): T
If DispatchExts
are provided, dispatch
will support an
additional signature.
1dispatch<R>( 2 asyncAction: ThunkAction<R, S, E, A>, 3): R
Returns the state S
of the mock store.
1getState(): S
Returns the actions A[]
of the mock store.
1getActions(): A[]
Clears the stored actions.
1clearActions(): void
Subscribe a listener
to the store.
1subscribe( 2 listener: (action: A) => void, 3): Redux.Unsubscribe
Because a mock store does not have or support reducers, this function will always throw an error.
1replaceReducer(nextReducer: Reducer<S, A>): never
Mock stores do not support reducers. Try supplying a function to
getStore
instead.
The @jedmao
scoped
version of this library is written in TypeScript and published with generated
type information. No need to npm install
additional
@types
. Additionally, a number of types and
interfaces (below) have been exported for your convenience.
If you provide DistpatchExts
then this type will return a
MockStoreEnhanced
, which supports async actions (e.g.,
thunks). Otherwise, it will just return a plain ol' MockStore
for sync actions.
1type MockStoreCreator< 2 S = {}, 3 A extends Action = AnyAction, 4 DispatchExts extends {} | void = void 5> = ( 6 state?: S | MockGetState<Redux.DeepPartial<S>>, 7) => DispatchExts extends void 8 ? MockStore<S, A> 9 : MockStoreEnhanced<S, A, DispatchExts>
This type is used in the MockStoreCreator
via
state?: S | MockGetState<S>
, which allows you to either supply a single state
object S
or a function that would return S
. Why a function? See
redux-mock-store#102.
1type MockGetState<S = {}> = (actions: AnyAction[]) => S
Enables async actions (e.g., thunks).
1type MockStoreEnhanced< 2 S, 3 A extends Action = AnyAction, 4 DispatchExts = {} 5> = MockStore<Redux.DeepPartial<S>, A> & { 6 dispatch: DispatchExts 7}
1interface MockStore<S = any, A extends Redux.Action = Redux.AnyAction> 2 extends Redux.Store<Redux.DeepPartial<S>, A> { 3 clearActions(): void 4 getActions(): A[] 5 subscribe(listener: (action: A) => void): Redux.Unsubscribe 6}
The MIT License
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
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
Project has not signed or included provenance with any releases.
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
52 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