#
Writing TestsBecause most of the Redux code you write are functions, and many of them are pure, they are easy to test without mocking.
#
Setting UpWe recommend Jest as the testing engine. Note that it runs in a Node environment, so you won't have access to the DOM.
To use it together with Babel, you will need to install babel-jest
:
and configure it to use babel-preset-env features in .babelrc
:
Then, add this to scripts
in your package.json
:
and run npm test
to run it once, or npm run test:watch
to test on every file change.
#
Action CreatorsIn Redux, action creators are functions which return plain objects. When testing action creators, we want to test whether the correct action creator was called and also whether the right action was returned.
#
Examplecan be tested like:
#
Async Action CreatorsFor async action creators using Redux Thunk or other middleware, it's best to completely mock the Redux store for tests. You can apply the middleware to a mock store using redux-mock-store. You can also use fetch-mock to mock the HTTP requests.
#
Examplecan be tested like:
#
ReducersA reducer should return the new state after applying the action to the previous state, and that's the behavior tested below.
#
Examplecan be tested like:
#
ComponentsA nice thing about React components is that they are usually small and only rely on their props. That makes them easy to test.
First, we will install React Testing Library. React Testing Library is a simple and complete React DOM testing utilities that encourage good testing practices. It uses react-dom's render
function and act
from react-dom/tests-utils.
If you are using jest as recommended above, we also recommend installing jest-dom as it provides a set of custom jest matchers that you can use to extend jest. These will make your tests more declarative, clear to read and to maintain. jest-dom is being used in the examples below.
To test the components, we render
them into the DOM and pass stubbed callbacks as props, then we assert whether the callbacks were called when expected.
#
Examplecan be tested like:
#
Connected ComponentsIf you use a library like React Redux, you might be using higher-order components like connect()
. This lets you inject Redux state into a regular React component.
Consider the following App
component:
To test it, we can use the wrapper
option in React Testing Library's render
function and export our own render
function as explained in React Testing Library's setup docs.
Our render
function can look like this:
And our test can use our exported render
function:
#
MiddlewareMiddleware functions wrap behavior of dispatch
calls in Redux, so to test this modified behavior we need to mock the behavior of the dispatch
call.
#
ExampleFirst, we'll need a middleware function. This is similar to the real redux-thunk.
We need to create a fake getState
, dispatch
, and next
functions. We use jest.fn()
to create stubs, but with other test frameworks you would likely use Sinon.
The invoke function runs our middleware in the same way Redux does.
We test that our middleware is calling the getState
, dispatch
, and next
functions at the right time.
In some cases, you will need to modify the create
function to use different mock implementations of getState
and next
.
#
GlossaryReact Testing Library: React Testing Library is a very light-weight solution for testing React components. It provides light utility functions on top of react-dom and react-dom/test-utils, in a way that encourages better testing practices. Its primary guiding principle is: "The more your tests resemble the way your software is used, the more confidence they can give you."
React Test Utils: ReactTestUtils makes it easy to test React components in the testing framework of your choice. React Testing Library uses the
act
function exported by React Test Utils.