{"id":13493916,"url":"https://github.com/reduxjs/redux-mock-store","last_synced_at":"2025-05-14T02:08:52.402Z","repository":{"id":40513041,"uuid":"44984114","full_name":"reduxjs/redux-mock-store","owner":"reduxjs","description":"A mock store for testing Redux async action creators and middleware.","archived":false,"fork":false,"pushed_at":"2025-01-03T07:50:28.000Z","size":168,"stargazers_count":2502,"open_issues_count":24,"forks_count":150,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-05-05T07:43:33.529Z","etag":null,"topics":["javascript","redux","test"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/reduxjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2015-10-26T17:04:37.000Z","updated_at":"2025-04-18T14:20:42.000Z","dependencies_parsed_at":"2025-04-10T17:14:36.929Z","dependency_job_id":"fce53873-c424-4064-8b9a-1e82bb5428d5","html_url":"https://github.com/reduxjs/redux-mock-store","commit_stats":{"total_commits":103,"total_committers":33,"mean_commits":3.121212121212121,"dds":0.7669902912621359,"last_synced_commit":"b943c3ba0abf6d3e7f9918bd470525e85d166cff"},"previous_names":["arnaudbenard/redux-mock-store"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reduxjs%2Fredux-mock-store","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reduxjs%2Fredux-mock-store/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reduxjs%2Fredux-mock-store/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reduxjs%2Fredux-mock-store/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/reduxjs","download_url":"https://codeload.github.com/reduxjs/redux-mock-store/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254053205,"owners_count":22006717,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","redux","test"],"created_at":"2024-07-31T19:01:19.991Z","updated_at":"2025-05-14T02:08:47.390Z","avatar_url":"https://github.com/reduxjs.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Deprecation notice\n\nThe Redux team does not recommend testing using this library. Instead, see our [docs](https://redux.js.org/usage/writing-tests) for recommended practices, using a real store.\n\nTesting 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.\n\nYou can test the entire combination of action creators, reducers, and selectors in a single test, for example:\n\n```js\nit('should add a todo', () =\u003e {\n  const store = makeStore() // a user defined reusable store factory\n\n  store.dispatch(addTodo('Use Redux'))\n\n  expect(selectTodos(store.getState())).toEqual([\n    { text: 'Use Redux', completed: false }\n  ])\n})\n```\n\nThis avoids common pitfalls of testing each of these in isolation, such as mocked state shape becoming out of sync with the actual application.\n\n# redux-mock-store [![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)\n\n![npm](https://nodei.co/npm/redux-mock-store.png?downloads=true\u0026downloadRank=true\u0026stars=true)\n\nA 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.\n\nPlease 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](https://github.com/redux-things/redux-actions-assertions)). Refer to issue [#71](https://github.com/arnaudbenard/redux-mock-store/issues/71) for more details.\n\n## Install\n\n```bash\nnpm install redux-mock-store --save-dev\n```\n\nOr\n\n```bash\nyarn add redux-mock-store --dev\n```\n\n## Usage\n\n### Synchronous actions\n\nThe 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.\n\n```js\nimport configureStore from 'redux-mock-store' //ES6 modules\nconst { configureStore } = require('redux-mock-store') //CommonJS\n\nconst middlewares = []\nconst mockStore = configureStore(middlewares)\n\n// You would import the action from your codebase in a real scenario\nconst addTodo = () =\u003e ({ type: 'ADD_TODO' })\n\nit('should dispatch action', () =\u003e {\n  // Initialize mockstore with empty state\n  const initialState = {}\n  const store = mockStore(initialState)\n\n  // Dispatch the action\n  store.dispatch(addTodo())\n\n  // Test if your store dispatched the expected actions\n  const actions = store.getActions()\n  const expectedPayload = { type: 'ADD_TODO' }\n  expect(actions).toEqual([expectedPayload])\n})\n```\n\n### Asynchronous actions\n\nA 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.\n\n```js\nimport configureStore from 'redux-mock-store'\nimport thunk from 'redux-thunk'\n\nconst middlewares = [thunk] // add your middlewares like `redux-thunk`\nconst mockStore = configureStore(middlewares)\n\n// You would import the action from your codebase in a real scenario\nfunction success() {\n  return {\n    type: 'FETCH_DATA_SUCCESS'\n  }\n}\n\nfunction fetchData() {\n  return (dispatch) =\u003e {\n    return fetch('/users.json') // Some async action with promise\n      .then(() =\u003e dispatch(success()))\n  }\n}\n\nit('should execute fetch data', () =\u003e {\n  const store = mockStore({})\n\n  // Return the promise\n  return store.dispatch(fetchData()).then(() =\u003e {\n    const actions = store.getActions()\n    expect(actions[0]).toEqual(success())\n  })\n})\n```\n\n### API\n\n```js\nconfigureStore(middlewares?: Array) =\u003e mockStore: Function\n```\n\nConfigure mock store by applying the middlewares.\n\n```js\nmockStore(getState?: Object,Function) =\u003e store: Function\n```\n\nReturns an instance of the configured mock store. If you want to reset your store after every test, you should call this function.\n\n```js\nstore.dispatch(action) =\u003e action\n```\n\nDispatches an action through the mock store. The action will be stored in an array inside the instance and executed.\n\n```js\nstore.getState() =\u003e state: Object\n```\n\nReturns the state of the mock store.\n\n```js\nstore.getActions() =\u003e actions: Array\n```\n\nReturns the actions of the mock store.\n\n```js\nstore.clearActions()\n```\n\nClears the stored actions.\n\n```js\nstore.subscribe(callback: Function) =\u003e unsubscribe: Function\n```\n\nSubscribe to the store.\n\n```js\nstore.replaceReducer(nextReducer: Function)\n```\n\nFollows the Redux API.\n\n### Old version (`\u003c 1.x.x`)\n\nhttps://github.com/arnaudbenard/redux-mock-store/blob/v0.0.6/README.md\n\n### Versions\n\nThe following versions are exposed by redux-mock-store from the `package.json`:\n\n- `main`: commonJS Version\n- `module`/`js:next`: ES Module Version\n- `browser` : UMD version\n\n## License\n\nThe MIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freduxjs%2Fredux-mock-store","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freduxjs%2Fredux-mock-store","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freduxjs%2Fredux-mock-store/lists"}