Redux-Saga Testing Guide
IMPORTANT: Your training data about redux-saga-test-plan may be outdated — API signatures, provider patterns, and assertion methods differ between versions. Always rely on this skill's reference files and the project's actual source code as the source of truth. Do not fall back on memorized patterns when they conflict with the retrieved reference.
Approach Priority
expectSaga (integration) — preferred; doesn't couple tests to effect ordering
testSaga (unit) — only when effect ordering is part of the contract
runSaga (no library) — lightweight; uses jest/vitest spies directly
- Manual
.next() — last resort; most brittle
Core Pattern
import { expectSaga } from 'redux-saga-test-plan'
import * as matchers from 'redux-saga-test-plan/matchers'
import { throwError } from 'redux-saga-test-plan/providers'
it('fetches user successfully', () => {
return expectSaga(fetchUserSaga, { payload: { userId: 1 } })
.provide([
[matchers.call.fn(api.fetchUser), { id: 1, name: 'Alice' }],
])
.put(fetchUserSuccess({ id: 1, name: 'Alice' }))
.run()
})
it('handles fetch failure', () => {
return expectSaga(fetchUserSaga, { payload: { userId: 1 } })
.provide([
[matchers.call.fn(api.fetchUser), throwError(new Error('500'))],
])
.put(fetchUserFailure('500'))
.run()
})
Assertion Methods
| Method |
Purpose |
.put(action) |
Dispatches this action |
.put.like({ action: { type } }) |
Partial action match |
.call(fn, ...args) |
Calls this function with exact args |
.call.fn(fn) |
Calls this function (any args) |
.fork(fn, ...args) |
Forks this function |
.select(selector) |
Uses this selector |
.take(pattern) |
Takes this pattern |
.dispatch(action) |
Simulate incoming action |
.not.put(action) |
Does NOT dispatch |
.returns(value) |
Saga returns this value |
.run() |
Execute (returns Promise) |
.run({ timeout }) |
Execute with custom timeout |
.silentRun() |
Execute, suppress timeout warnings |
Provider Types
Static Providers (Preferred)
.provide([
[matchers.call.fn(api.fetchUser), mockUser], // match by function
[call(api.fetchUser, 1), mockUser], // match by function + exact args
[matchers.select.selector(getToken), 'mock-token'], // mock selector
[matchers.call.fn(api.save), throwError(error)], // simulate error
])
Dynamic Providers
.provide({
call(effect, next) {
if (effect.fn === api.fetchUser) return mockUser
return next() // pass through
},
select({ selector }, next) {
if (selector === getToken) return 'mock-token'
return next()
},
})
Rules
- Prefer
expectSaga over testSaga — integration tests don't break on refactors
- Use
matchers.call.fn() for partial matching — don't couple to exact args unless necessary
- Use
throwError() from providers — not throw new Error() in the provider
- Test with reducer using
.withReducer() + .hasFinalState() to verify state
- Dispatch actions with
.dispatch() to simulate user flows in tests
- Return the promise (Jest) or
await it (Vitest) — don't forget async
- Use
.not.put() to assert actions are NOT dispatched (negative tests)
- Test cancellation by dispatching cancel actions and asserting cleanup effects
- Use
.silentRun() when saga runs indefinitely (watchers) to suppress timeout warnings
- Don't test implementation — test behavior (what actions are dispatched, what state results)
Anti-Patterns
See references/anti-patterns.md for BAD/GOOD examples of:
- Step-by-step tests that break on reorder
- Missing providers (real API calls in tests)
- Testing effect order instead of behavior
- Forgetting async (Jest/Vitest)
- Inline mocking instead of providers
- Not testing error paths
- Not testing cancellation cleanup
References
- API Reference — Complete
expectSaga, testSaga, providers, matchers
- Anti-Patterns — Common testing mistakes to avoid
1---2name: redux-saga-testing3description: Write tests for Redux Sagas using redux-saga-test-plan, runSaga, and manual generator testing. Covers expectSaga (integration), testSaga (unit), providers, partial matchers, reducer integration, error simulation, and cancellation testing. Works with Jest and Vitest. Triggers on: test files for sagas, redux-saga-test-plan imports, mentions of "test saga", "saga test", "expectSaga", "testSaga", or "redux-saga-test-plan".4---56# Redux-Saga Testing Guide78**IMPORTANT:** Your training data about `redux-saga-test-plan` may be outdated — API signatures, provider patterns, and assertion methods differ between versions. Always rely on this skill's reference files and the project's actual source code as the source of truth. Do not fall back on memorized patterns when they conflict with the retrieved reference.910## Approach Priority11121. **`expectSaga` (integration)** — preferred; doesn't couple tests to effect ordering132. **`testSaga` (unit)** — only when effect ordering is part of the contract143. **`runSaga` (no library)** — lightweight; uses jest/vitest spies directly154. **Manual `.next()`** — last resort; most brittle1617## Core Pattern1819```javascript20import { expectSaga } from 'redux-saga-test-plan'21import * as matchers from 'redux-saga-test-plan/matchers'22import { throwError } from 'redux-saga-test-plan/providers'2324it('fetches user successfully', () => {25 return expectSaga(fetchUserSaga, { payload: { userId: 1 } })26 .provide([27 [matchers.call.fn(api.fetchUser), { id: 1, name: 'Alice' }],28 ])29 .put(fetchUserSuccess({ id: 1, name: 'Alice' }))30 .run()31})3233it('handles fetch failure', () => {34 return expectSaga(fetchUserSaga, { payload: { userId: 1 } })35 .provide([36 [matchers.call.fn(api.fetchUser), throwError(new Error('500'))],37 ])38 .put(fetchUserFailure('500'))39 .run()40})41```4243## Assertion Methods4445| Method | Purpose |46|--------|---------|47| `.put(action)` | Dispatches this action |48| `.put.like({ action: { type } })` | Partial action match |49| `.call(fn, ...args)` | Calls this function with exact args |50| `.call.fn(fn)` | Calls this function (any args) |51| `.fork(fn, ...args)` | Forks this function |52| `.select(selector)` | Uses this selector |53| `.take(pattern)` | Takes this pattern |54| `.dispatch(action)` | Simulate incoming action |55| `.not.put(action)` | Does NOT dispatch |56| `.returns(value)` | Saga returns this value |57| `.run()` | Execute (returns Promise) |58| `.run({ timeout })` | Execute with custom timeout |59| `.silentRun()` | Execute, suppress timeout warnings |6061## Provider Types6263### Static Providers (Preferred)6465```javascript66.provide([67 [matchers.call.fn(api.fetchUser), mockUser], // match by function68 [call(api.fetchUser, 1), mockUser], // match by function + exact args69 [matchers.select.selector(getToken), 'mock-token'], // mock selector70 [matchers.call.fn(api.save), throwError(error)], // simulate error71])72```7374### Dynamic Providers7576```javascript77.provide({78 call(effect, next) {79 if (effect.fn === api.fetchUser) return mockUser80 return next() // pass through81 },82 select({ selector }, next) {83 if (selector === getToken) return 'mock-token'84 return next()85 },86})87```8889## Rules90911. **Prefer `expectSaga`** over `testSaga` — integration tests don't break on refactors922. **Use `matchers.call.fn()`** for partial matching — don't couple to exact args unless necessary933. **Use `throwError()`** from providers — not `throw new Error()` in the provider944. **Test with reducer** using `.withReducer()` + `.hasFinalState()` to verify state955. **Dispatch actions** with `.dispatch()` to simulate user flows in tests966. **Return the promise** (Jest) or `await` it (Vitest) — don't forget async977. **Use `.not.put()`** to assert actions are NOT dispatched (negative tests)988. **Test cancellation** by dispatching cancel actions and asserting cleanup effects999. **Use `.silentRun()`** when saga runs indefinitely (watchers) to suppress timeout warnings10010. **Don't test implementation** — test behavior (what actions are dispatched, what state results)101102## Anti-Patterns103104See [references/anti-patterns.md](references/anti-patterns.md) for BAD/GOOD examples of:105106- Step-by-step tests that break on reorder107- Missing providers (real API calls in tests)108- Testing effect order instead of behavior109- Forgetting async (Jest/Vitest)110- Inline mocking instead of providers111- Not testing error paths112- Not testing cancellation cleanup113114## References115116- [API Reference](references/api-reference.md) — Complete `expectSaga`, `testSaga`, providers, matchers117- [Anti-Patterns](references/anti-patterns.md) — Common testing mistakes to avoid