Registry / testing / redux-test-utils

redux-test-utils

JSON →
library1.0.2jsnpmunverified

redux-test-utils provides a focused set of utilities designed to simplify the testing of Redux-connected components and logic. It offers `createMockStore` and `createMockDispatch` functions, which produce mock objects that mimic the behavior of a Redux store and dispatch function, respectively. These mocks capture dispatched actions, allowing developers to easily assert against action types, payloads, and dispatch order using intuitive methods like `getAction`, `getActions`, `isActionDispatched`, and `isActionTypeDispatched`. The library is currently at version 1.0.2, with its 1.0.0 release marking a complete rewrite in TypeScript, enhancing type safety and developer experience. While there isn't a strict release cadence, updates address compatibility and improvements. Its key differentiator lies in its Redux-specific API, which offers a more streamlined and less verbose testing approach compared to general-purpose mocking libraries for Redux scenarios.

npm install redux-test-utils
INSTALL
IMPORT
SIG · REDUX-TEST-UTILS
R
redux-test-utils
testingjavascriptv1.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

createMockStore
import { createMockStore } from 'redux-test-utils';
const createMockStore = require('redux-test-utils').createMockStore;
The library primarily uses ES Module syntax. CommonJS require() might not work as expected since v1.0.0 due to TypeScript rewrite.
createMockDispatch
import { createMockDispatch } from 'redux-test-utils';
const { createMockDispatch } = require('redux-test-utils');
Ensure your project is configured for ESM imports, especially in testing environments.
MockStore
import type { MockStore } from 'redux-test-utils';
TypeScript users can import specific types for better type checking.

This quickstart demonstrates how to create a mock Redux store, dispatch actions, and use the utility methods to assert on the dispatched actions and the store's state.

import { createMockStore } from 'redux-test-utils'; describe('Redux Mock Store Example', () => { const initialState = { user: { name: 'John Doe', id: '123' }, settings: { theme: 'dark' } }; it('should correctly capture dispatched actions and maintain state', () => { const store = createMockStore(initialState); const loginAction = { type: 'USER_LOGIN', payload: { username: 'jdoe' } }; const themeChangeAction = { type: 'SET_THEME', payload: 'light' }; store.dispatch(loginAction); store.dispatch(themeChangeAction); expect(store.getAction(loginAction.type)).toEqual(loginAction); expect(store.getActions()).toEqual([loginAction, themeChangeAction]); expect(store.isActionDispatched(loginAction)).toBe(true); expect(store.isActionTypeDispatched('SET_THEME')).toBe(true); expect(store.getState()).toBe(initialState); }); it('should reset actions when a new mock store is created', () => { const store1 = createMockStore({}); store1.dispatch({ type: 'TEST_ACTION_1' }); expect(store1.getActions().length).toBe(1); const store2 = createMockStore({}); expect(store2.getActions().length).toBe(0); }); });
Debug
Known issues
breakingVersion 1.0.0 introduced a complete rewrite of the codebase in TypeScript. While the library claims 'Same functionality', users of older versions might experience breaking changes related to module resolution, type definitions, or toolchain compatibility, especially if not using TypeScript.
fix
Ensure your project uses a bundler or test runner that correctly handles ES Modules and TypeScript. Update TypeScript configuration if necessary to include the library's types.
affects: >=1.0.0
gotchaThis library relies on 'redux' as a peer dependency. You must install a compatible version of 'redux' alongside 'redux-test-utils' for it to function correctly.
fix
Install 'redux' with `npm install redux@^4.0.0` or `yarn add redux@^4.0.0`.
affects: >=1.0.0
gotchaThe mock store and dispatch objects are designed for unit testing specific Redux logic. They do not fully simulate the Redux middleware chain or complex reducer logic. For integration testing or more complex scenarios, consider using actual Redux stores with testing utilities like `@reduxjs/toolkit/query/react/testing` or `redux-mock-store` for middleware support.
fix
Understand the scope of `redux-test-utils` for simple action/state assertions. For middleware, use `redux-mock-store`; for full integration, set up a real store.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (0 , redux_test_utils_1.createMockStore) is not a function
This error typically occurs when attempting to `require()` an ES Module in a CommonJS context without proper transpilation, or when using an incompatible bundler/test environment configuration.
fix
Ensure you are using `import { createMockStore } from 'redux-test-utils';`. If in a Node.js CJS environment, configure your test runner (e.g., Jest) to transpile ES modules, or use an ESM-compatible setup.
Error: Cannot find module 'redux-test-utils'
The package is not installed, or your module resolution paths are incorrectly configured.
fix
Install the package using `npm install redux-test-utils --save-dev` or `yarn add redux-test-utils --dev`. Verify your `tsconfig.json` (if applicable) and build tool configurations.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies
reduxrequiredRequired as a peer dependency for Redux applications.
Agent activity
2 hits · last 30 days
node
2
Resources
redux-test-utils — npm install redux-test-utils · libregistry