Redux Saga Test Engine

3.0.0 · active · verified Sun Apr 19

redux-saga-test-engine is a testing utility designed to simplify the process of testing Redux Saga generator functions. It provides a structured API for collecting specific Redux Saga effects (like `PUT` and `CALL`) and mapping yielded effects to predefined return values, eliminating the need for manual iteration over generator steps. The current stable version is 3.0.0. While no explicit release cadence is stated, major versions introduce breaking changes to the top-level API, with minor versions providing feature enhancements and bug fixes. It differentiates itself from alternatives like `redux-saga-test` and `redux-saga-test-plan` by offering a distinct approach to effect collection and environment stubbing, aiming for a more direct and less verbose testing experience, particularly when focusing on the effects a saga produces rather than its step-by-step execution.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `createSagaTestEngine` to test a Redux Saga, including how to collect specific effect types and provide mock responses for `select` and `call` effects, effectively isolating the saga logic.

import { createSagaTestEngine } from 'redux-saga-test-engine';
import { select, call, put } from 'redux-saga/effects';

// Mock API and selectors for the example
const API = {
  doWeLovePuppies: () => ({ answer: 'Of course we do!' })
};
const getPuppy = () => ({ barks: true, cute: 'Definitely' });
const petPuppy = (puppy) => ({ type: 'PET_PUPPY', payload: puppy });
const hugPuppy = (puppy) => ({ type: 'HUG_PUPPY', payload: puppy });

// The saga to be tested
function* sagaToTest(action) {
  const puppyState = yield select(getPuppy);
  const apiResponse = yield call(API.doWeLovePuppies);

  if (puppyState.cute && apiResponse.answer) {
    yield put(petPuppy(puppyState));
    yield put(hugPuppy(puppyState));
  }
}

// Choose which effect types you want to collect from the saga.
const collectEffects = createSagaTestEngine(['PUT', 'CALL']);

// Define environment mappings and initial action
const initialAction = { type: 'START_SAGA' };
const envMapping = [
  [select(getPuppy), { barks: true, cute: 'Definitely' }],
  [call(API.doWeLovePuppies), { answer: 'Of course we do!' }]
];

const actualEffects = collectEffects(
  sagaToTest,
  envMapping,
  initialAction
);

console.log(JSON.stringify(actualEffects, null, 2));
/*
Expected output (simplified):
[
  { "@@redux-saga/IO": true, "call": { "args": [], "fn": {} } }, // call(API.doWeLovePuppies)
  { "@@redux-saga/IO": true, "put": { "action": { "type": "PET_PUPPY", "payload": { "barks": true, "cute": "Definitely" } } } },
  { "@@redux-saga/IO": true, "put": { "action": { "type": "HUG_PUPPY", "payload": { "barks": true, "cute": "Definitely" } } } }
]
*/

view raw JSON →