---
title: "Testing React Native With the New Jest - Part II"
description: "A step by step guide to testing React Native with the Jest. Part Two - Redux: Snapshots for your actions and reducers."
url: https://www.callstack.com/blog/testing-react-native-with-the-new-jest-part-2-redux-snapshots-for-your-actions-and-reducers
updated: 2026-10-24T08:39:38.099Z
---

# Testing React Native With the New Jest - Part II

A step by step guide to testing React Native with the Jest. Part Two - Redux: Snapshots for your actions and reducers.

_By Ferran Negre, Software Engineer @ Callstack · Published 2016-09-29 · Category: Testing_

It’s a well known paradigm in the React world for any big app you want to write to extract your business logic (aka manage the state of your app) out of your components. The majority of your components should be a small piece of reusable code that just renders UI based on the app state that is triggered mainly by: User input and API calls. The same paradigm is used in React Native.

I will talk about Redux together with React Native since testing Redux is easy. However feel free to apply the same concepts to another state management you choose. That’s what we will do here:

- Testing Reducers: Manual comparison vs. Snapshots
- Mock the built-in React Native fetch
- _Re-mock_ the redux-mock-store
- Testing Actions (including async ones): Manual comparison vs. Snapshots

Please refer to [Unit testing React Native with the \*new\* Jest (I)  ](https://www.callstack.com/blog/testing-react-native-with-the-new-jest-part-1-snapshots-come-into-play)for setting up Jest with React Native.

### Reducers

Testing a reducer might be one of the easiest parts to test from your app: Given the current state and an action, a reducer should return a new state after that action is applied. Let’s see how to [test part of this example](https://github.com/ferrannp/react-native-testing-example/blob/master/app/reducers/__tests__/repos-test.js):

[GitHub Gist](https://gist.github.com/ferrannp/7489844bd1a3ba3ecec4467339f0fe5c)

We will focus on testing that _LOAD\_REPOS.success_ action. Let’s create version 1 of our test:

[GitHub Gist](https://gist.github.com/ferrannp/50dc48e5206098cd78714ea47c51ca0b)

Copy & paste again :(

Quite short, right? I just went and copied and pasted the _initialState_from my reducer and added the part that changes (_repos_). When you work in larger projects and your state starts to grow (0r you do some refactoring), you will find this process to be so repetitive that you will want to do something better, so, version 2:

[GitHub Gist](https://gist.github.com/ferrannp/6c6ccdfd91aa90aba461033adac42ae2)

Reimplementing the reducer :(

This is better, right? I can add or delete anything to the state (except the _isLoading_ & _repos_ part) and I won’t break the test. I just need to go back here if that _action.success_ does actually affect another part of the state in the future. But, don’t you feel that test it is actually very similar to:

[GitHub Gist](https://gist.github.com/ferrannp/93c019484010d4bf0d8b3e21313655dc)

Again, what we are doing in the test is basically reimplementing the reducer. Anytime I change the logic in my reducer, I need to come back here and do the same. It get’s so repetitive… Can we do better? Let’s _snapshot_ that output. Hello version 3:

[GitHub Gist](https://gist.github.com/ferrannp/18bd87902b635f53827d7cf1656c2135)

Given the state and an action, generate the output for me

I know, it’s just one line and I did not have to repeat myself at all ([DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself)?). Do you like it? I do! Let’s see what the snapshot looks like:

[GitHub Gist](https://gist.github.com/ferrannp/5643ae88fdc58812ee1ba459734e4ccb)

Wow! I can actually see how my state will exactly look like after applying the action. So as you can see, we are forcing Jest to do the hard work while we are just watching. In this case, if you add or remove any part of your state, this test will break: Which is what you should expect, since the outputted state would be different. But as I explained in the [first part ](https://www.callstack.com/blog/testing-react-native-with-the-new-jest-part-1-snapshots-come-into-play/)of these series, the only thing we need to do here is:

- Review the Jest error output
- If it’s expected, run the test again using -u to re-generate the snapshot:

[GitHub Gist](https://gist.github.com/callstack-bot/253511a195c69012e768f4185996a6cc)

- If it’s not expected, go and fix it (obviously)

This is a great example of how fast and easy is to write your tests first, aka TDD ([Test-driven development](https://en.wikipedia.org/wiki/Test-driven_development)):

1. Write your test first
2. Start writing your reducer with the Jest _— watch_ mode enabled
3. Once the outputted snapshot is the one you are expecting, just tell Jest to save it
4. Done!

### Actions

A simple action creator is just a function that returns an object:

[GitHub Gist](https://gist.github.com/ferrannp/5bda212bcea9280344b563a7827c303f)

Simple action creator

Which can be tested like:

[GitHub Gist](https://gist.github.com/ferrannp/f287040f3813232511d07d99c9c2e903)

For the second part, let’s check out the generated snapshot:

[GitHub Gist](https://gist.github.com/ferrannp/4ebec01358e15156640ee95d9544fbe3)

Yea, the exact same thing as the reducer: I (and my team) can read the output instead of re-creating the action again (which includes the need to maintain that code in both places).

By the way, if you are already bored of reading, you can [watch the same concept](https://egghead.io/lessons/javascript-use-jest-s-snapshot-testing-feature) in a nice and short explanation thanks to [Kent C. Dodds](https://medium.com/u/db72389e89d8). If you are not, let’s continue. Since testing such simple actions is so _meh_, let’s go and test an async action that uses the built-in fetch of React Native and dispatches more than one action at once.

### Async actions

This is the async action we will test:

[GitHub Gist](https://gist.github.com/ferrannp/0d1478ae5530e75e43fe09dd43873cca)

We need some mocking help here to be able to have a nice way to test that so… Let’s mock!

#### **Mocking the built-in fetch of React Native**

Similar way that you can use [whatwg-fetch](https://github.com/github/fetch) in your web apps which will include a fetch polyfill into _window.fetch_, React Native uses the same idea and includes fetch into the global object as _global.fetch_.

We don’t want to make real api calls into our unit tests but we would like to mock the responses to write better assertions. There are some mock libraries out there but I just wrote a smaller version that fits my purposes:

[GitHub Gist](https://gist.github.com/ferrannp/6a69f6dbddcae8d4551a9f1e85a3a261)

Mocking fetch

Then, we can tell Jest to load that config using the package.json field _setupFiles_:

[GitHub Gist](https://gist.github.com/callstack-bot/0cd5e6152df991605b4cffbe692e025f)

You will see when we are testing our async action how I used this mock. For now, let’s continue mocking.

#### Mocking a Redux store

I am sure that a lot of Redux users are familiar with [redux-mock-store](https://github.com/arnaudbenard/redux-mock-store). It’s a simple helper that provides a mocked version of the Redux store and makes it very easy to test what actions have been called. Once you have your mocked store, you can use _store.dispatch_ and get the list of actions that has been dispatched using _store.getActions_.

But you know what? I feel so lazy that I don’t want to do the same in all my tests where I need a store and a thunk, so I am going to _re-mock_ the _redux-mock-store_ module. See, in Jest you can create a [\_\_mocks\_\_](https://facebook.github.io/jest/docs/manual-mocks.html)folder in the same level as your _node\_modules_ to mock any npm module. So I can actually create a _redux-mock-store.js_ file (name of the module) in the _\_\_mocks\_\__ folder:

[GitHub Gist](https://gist.github.com/ferrannp/bb8160411f3158d3090f2da2c8d08f87)

And use it in my tests like:

[GitHub Gist](https://gist.github.com/ferrannp/a0a9d4777c701f948a765fd904b0af9d)

Lazy, eh?

#### Async test

I feel that we are ready to test our async action now, don’t you? Basically what we want to test here is:

1. A successful request should trigger _request_ & _success_ actions
2. A failure request should trigger _request_ & _failure_ actions

First case:

[GitHub Gist](https://gist.github.com/ferrannp/d03fd7043eaf726b74696706fa5ba532)

Rather short, right?

And what output will be generated?

[GitHub Gist](https://gist.github.com/ferrannp/11844f8380fc328e5d0b7a0a8e0142ac)

Just as expected and in this case, I think this one is very useful and easy to read: Quick read by reading “type” and longer read by checking that actually the “data” matches what we told fetch to return.

The second case is very similar:

[GitHub Gist](https://gist.github.com/ferrannp/30873c91ce1b884a3635ea3898d8ce09)

And its output:

[GitHub Gist](https://gist.github.com/ferrannp/e68d1d7b9de28d62ef97103cdf635b39)

As you can see, when you have the mocks in place, writing this kind of tests become very straightforward, and most likely, you won’t need to mock anything since you can find them out there or write your own and reuse them for other projects.

## Wrap up

This is it, with this part and the first one, we pretty much covered (I hope) unit testing of your React Native components + your state management logic. We also re-discovered Jest and its new superpower: Snapshots. On contrary, I am sure the way to test React Native apps will continue to improve as much as Jest keeps growing. So this article might get out of date quite soon!

Let me finish with some personal thoughts:

### Why would I use Jest?

- You can easily test your React & React Native components
- Snapshots are a plus, but they are totally optional
- You can easily test Redux or other state management libraries with Jest: Shared logic between web and mobile _could_ share tests too!
- The same community have created React Native & Jest. What does that mean? They will be the ones who care the most about making React Native and Jest to play nicely together in the future

To sum up: If you test your web & mobile app with Jest, you can use the same core principle of React Native:

_Learn once, write anywhere — React Native_

Of course, that is only my opinion. I just hope you enjoyed this short journey about testing a React Native app with the _new_ Jest and remember… Happy testing!

You can also check our services offered by our [React Native development company](https://www.callstack.com/blog/testing-react-native-with-the-new-jest-part-2-redux-snapshots-for-your-actions-and-reducers).
