diff --git a/src/utils/combineReducers.js b/src/utils/combineReducers.js index a3f8e40dd4..b48d4f2da6 100644 --- a/src/utils/combineReducers.js +++ b/src/utils/combineReducers.js @@ -59,9 +59,9 @@ function assertReducerSanity(reducers) { if (typeof initialState === 'undefined') { throw new Error( `Reducer "${key}" returned undefined during initialization. ` + - `If the state passed to the reducer is undefined, you must ` + - `explicitly return the initial state. The initial state may ` + - `not be undefined.` + `Reducers should never return undefined. Make sure this reducer ` + + `has a catch-all clause for unknown action types and that it returns a ` + + `default initial state if the state passed to it is undefined.` ) } diff --git a/test/utils/combineReducers.spec.js b/test/utils/combineReducers.spec.js index 060ac0ee48..bfce2e98fd 100644 --- a/test/utils/combineReducers.spec.js +++ b/test/utils/combineReducers.spec.js @@ -84,6 +84,25 @@ describe('Utils', () => { ) }) + it('throws an error if reducer does not have a catch-all clause for unknown action types', () => { + const reducer = combineReducers({ + counter(state = 0, action) { + switch (action.type) { + case 'increment': + return state + 1 + case 'decrement': + return state - 1 + case undefined: + return state + } + } + }) + + expect(() => reducer()).toThrow( + /"counter".*initialization/ + ) + }) + it('catches error thrown in reducer when initializing and re-throw', () => { const reducer = combineReducers({ throwingReducer() { @@ -151,7 +170,7 @@ describe('Utils', () => { expect(reducer(initialState, { type: 'increment' })).toNotBe(initialState) }) - it('throws an error if reducer does not return current state for all unknown action types', () => { + it('throws an error if reducer does not return current state for unknown action types', () => { const reducer = combineReducers({ counter(state, action) { switch (action.type) {