All Projects → hbarcelos → better-redux-tests

hbarcelos / better-redux-tests

Licence: Unlicense license
Source code for my article "A better approach for testing your Redux code"

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to better-redux-tests

Selenium.WebDriver.Extensions
Extensions for Selenium WebDriver including jQuery/Sizzle selector support.
Stars: ✭ 46 (+283.33%)
Mutual labels:  selectors
necktie
Necktie – a simple DOM binding tool
Stars: ✭ 43 (+258.33%)
Mutual labels:  selectors
hanbi
A small javascript library for stubbing and spying on methods/functions.
Stars: ✭ 49 (+308.33%)
Mutual labels:  mocks
elquery
Read and manipulate HTML in emacs
Stars: ✭ 32 (+166.67%)
Mutual labels:  selectors
aem-stubs
Tool for providing sample data for AEM applications in a simple and flexible way. Stubbing server on AEM, no separate needed.
Stars: ✭ 40 (+233.33%)
Mutual labels:  mocks
observer-spy
This library makes RxJS Observables testing easy!
Stars: ✭ 310 (+2483.33%)
Mutual labels:  mocks
Mockito
Most popular Mocking framework for unit tests written in Java
Stars: ✭ 12,453 (+103675%)
Mutual labels:  mocks
ReduxSimple
Simple Stupid Redux Store using Reactive Extensions
Stars: ✭ 119 (+891.67%)
Mutual labels:  selectors
ego
Alter Ego: run Linux desktop applications under a different local user
Stars: ✭ 90 (+650%)
Mutual labels:  isolation
testcafe-vue-selectors
TestCafe selector extensions for Vue.js apps.
Stars: ✭ 103 (+758.33%)
Mutual labels:  selectors
zedux
⚡ A high-level, declarative, composable form of Redux https://bowheart.github.io/zedux/
Stars: ✭ 43 (+258.33%)
Mutual labels:  selectors
Unmockable
💉 ↪️ 🎁 Unmockable objects wrapping in .NET
Stars: ✭ 35 (+191.67%)
Mutual labels:  mocks
mockingbird
🐦 Decorator Powered TypeScript Library for Creating Mocks
Stars: ✭ 70 (+483.33%)
Mutual labels:  mocks
coredux
Dualism to Redux. Two-way combining of redux modules
Stars: ✭ 14 (+16.67%)
Mutual labels:  selectors
selectorlib
A library to read a YML file with Xpath or CSS Selectors and extract data from HTML pages using them
Stars: ✭ 53 (+341.67%)
Mutual labels:  selectors
Mockery
A mock code autogenerator for Golang
Stars: ✭ 3,138 (+26050%)
Mutual labels:  mocks
dont-waste-your-ducking-time
🐓 An opinionated guide on how to test Redux ducks
Stars: ✭ 28 (+133.33%)
Mutual labels:  selectors
Example
Metarhia application example for Node.js
Stars: ✭ 147 (+1125%)
Mutual labels:  isolation
temperjs
State management for React, made simple.
Stars: ✭ 15 (+25%)
Mutual labels:  selectors
node-mock-factory
🐋 node-mock-factory
Stars: ✭ 26 (+116.67%)
Mutual labels:  mocks

Better Redux Tests

This repository contains my take on how to test Redux applications.

It contains the source code related to this article in my blog:
A better approach for testing your Redux code

TL;DR

A few thoughts about how to approach Redux testing:

Vanilla Redux

  • The smallest standalone unit in Redux is the entire state slice. Unit tests should interact with it as a whole.
  • There is no point in testing reducers, action creators and selectors in isolation. As they are tightly coupled with each other, isolation gives us little to no value.
  • Tests should interact with your Redux slice same way your application will.
    • Always use action creators to dispatch actions.
    • Always use selectors to read from the state when performing assertions.
    • By doing that, you will be able to catch errors in both your action creators and selectors, without having to write tests targeting them in isolation.
  • Avoid assertions like toEqual/toDeepEqual against the state object, as they create a coupling between your tests and the state structure.
    • Moving state around is quite common in Redux applications. By avoiding this kind of coupling you save yourself from a maintenance nightmare.
  • Using selectors gives you the granularity you need to run simple assertions.
    • If a given piece of state is not touched after an action is dispatched, you don't need to reference it in your test.
  • Selectors and action creators should be boring, so they won't require testing.
    • If you need something more elaborate, see if reselect can help you with your selectors and if redux-act or @reduxjs/toolkit can ease the pain when creating actions before adding complexity by yourself.
  • Your slice is somewhat equivalent to a pure function, which means you don't need any mocking facilities in order to test it.

Redux + redux-thunk

  • Dispatching thunks doesn't have any direct effect. Only after the thunk is called is when we will have the side-effects we need to make our application work.
  • Here you can use stubs, spies and sometimes mocks (but don't abuse mocks).
  • Because of the way thunks are structured, the only way to test them is by testing their implementation details.
    • The downside to this is that your tests are more coupled with your code, so it will be harder to maintain.
  • The strategy when testing thunks is to setup the store, dispatch the thunk and then asserting whether it dispatched the actions you expected in the order you expected or not.

Using This Repo

Structure

Here's how this repo is structured:

src
├── api
│   └── index.js
└── modules
    ├── auth
    │   ├── authSlice.js
    │   └── authSlice.test.js
    └── documents
        ├── documentsSlice.js
        └── documentsSlice.test.js

The auth module is very simple, making it easier to familiarize yourself with the ideas discussed here.

The documents module looks more like a "real world" application, which makes it a little trickier to test, but the same ideas apply.

Running the Tests

First, don't forget to install the dependencies:

yarn install

Then run:

yarn test --verbose
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].