Gathering detailed insights and metrics for redux-mock-store
Gathering detailed insights and metrics for redux-mock-store
Gathering detailed insights and metrics for redux-mock-store
Gathering detailed insights and metrics for redux-mock-store
@types/redux-mock-store
TypeScript definitions for redux-mock-store
redux-mock-store-jest
[![Circle CI](https://circleci.com/gh/arnaudbenard/redux-mock-store/tree/master.svg?style=svg)](https://circleci.com/gh/arnaudbenard/redux-mock-store/tree/master)
ember-redux-mock-store-shim
Redux Mock Store for Ember apps
@jedmao/redux-mock-store
A mock store for testing your redux async action creators and middleware
A mock store for testing Redux async action creators and middleware.
npm install redux-mock-store
97.9
Supply Chain
100
Quality
81.5
Maintenance
100
Vulnerability
99.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,501 Stars
140 Commits
149 Forks
20 Watching
6 Branches
38 Contributors
Updated on 14 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-8.1%
231,048
Compared to previous day
Last week
1.5%
1,265,587
Compared to previous week
Last month
6.9%
5,365,923
Compared to previous month
Last year
-1%
60,935,499
Compared to previous year
1
1
The Redux team does not recommend testing using this library. Instead, see our docs for recommended practices, using a real store.
Testing with a mock store leads to potentially confusing behaviour, such as state not updating when actions are dispatched. Additionally, it's a lot less useful to assert on the actions dispatched rather than the observable state changes.
You can test the entire combination of action creators, reducers, and selectors in a single test, for example:
1it('should add a todo', () => { 2 const store = makeStore() // a user defined reusable store factory 3 4 store.dispatch(addTodo('Use Redux')) 5 6 expect(selectTodos(store.getState())).toEqual([ 7 { text: 'Use Redux', completed: false } 8 ]) 9})
This avoids common pitfalls of testing each of these in isolation, such as mocked state shape becoming out of sync with the actual application.
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.
Please note that this library is designed to test the action-related logic, not the reducer-related one. In other words, 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 #71 for more details.
1npm install redux-mock-store --save-dev
Or
1yarn add redux-mock-store --dev
The simplest usecase is for synchronous actions. In this example, we will test if the addTodo
action returns the right payload. redux-mock-store
saves all the dispatched actions inside the store instance. You can get all the actions by calling store.getActions()
. Finally, you can use any assertion library to test the payload.
1import configureStore from 'redux-mock-store' //ES6 modules 2const { configureStore } = require('redux-mock-store') //CommonJS 3 4const middlewares = [] 5const mockStore = configureStore(middlewares) 6 7// You would import the action from your codebase in a real scenario 8const addTodo = () => ({ type: 'ADD_TODO' }) 9 10it('should dispatch action', () => { 11 // Initialize mockstore with empty state 12 const initialState = {} 13 const store = mockStore(initialState) 14 15 // Dispatch the action 16 store.dispatch(addTodo()) 17 18 // Test if your store dispatched the expected actions 19 const actions = store.getActions() 20 const expectedPayload = { type: 'ADD_TODO' } 21 expect(actions).toEqual([expectedPayload]) 22})
A common usecase for an asynchronous action is a HTTP request to a server. In order to test those types of actions, you will need to call store.getActions()
at the end of the request.
1import configureStore from 'redux-mock-store' 2import thunk from 'redux-thunk' 3 4const middlewares = [thunk] // add your middlewares like `redux-thunk` 5const mockStore = configureStore(middlewares) 6 7// You would import the action from your codebase in a real scenario 8function success() { 9 return { 10 type: 'FETCH_DATA_SUCCESS' 11 } 12} 13 14function fetchData() { 15 return (dispatch) => { 16 return fetch('/users.json') // Some async action with promise 17 .then(() => dispatch(success())) 18 } 19} 20 21it('should execute fetch data', () => { 22 const store = mockStore({}) 23 24 // Return the promise 25 return store.dispatch(fetchData()).then(() => { 26 const actions = store.getActions() 27 expect(actions[0]).toEqual(success()) 28 }) 29})
1configureStore(middlewares?: Array) => mockStore: Function
Configure mock store by applying the middlewares.
1mockStore(getState?: Object,Function) => store: Function
Returns an instance of the configured mock store. If you want to reset your store after every test, you should call this function.
1store.dispatch(action) => action
Dispatches an action through the mock store. The action will be stored in an array inside the instance and executed.
1store.getState() => state: Object
Returns the state of the mock store.
1store.getActions() => actions: Array
Returns the actions of the mock store.
1store.clearActions()
Clears the stored actions.
1store.subscribe(callback: Function) => unsubscribe: Function
Subscribe to the store.
1store.replaceReducer(nextReducer: Function)
Follows the Redux API.
< 1.x.x
)https://github.com/arnaudbenard/redux-mock-store/blob/v0.0.6/README.md
The following versions are exposed by redux-mock-store from the package.json
:
main
: commonJS Versionmodule
/js:next
: ES Module Versionbrowser
: UMD versionThe MIT License
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 10/18 approved changesets -- score normalized to 5
Reason
7 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 5
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
56 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
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