Skip to content

Commit c169608

Browse files
committed
Warns when type of reducers passed is invalid (not a function) to avoid silent failure
1 parent 9e51c17 commit c169608

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/combineReducers.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,11 @@ export default function combineReducers(reducers) {
106106
const key = reducerKeys[i]
107107

108108
if (process.env.NODE_ENV !== 'production') {
109-
if (typeof reducers[key] === 'undefined') {
109+
const reducerUnderInspection = reducers[key]
110+
if (typeof reducerUnderInspection === 'undefined') {
110111
warning(`No reducer provided for key "${key}"`)
112+
} else if (typeof reducerUnderInspection !== 'function') {
113+
warning(`Reducer provided for "${key}" is not a function. Received a ${typeof key}.`)
111114
}
112115
}
113116

test/combineReducers.spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,30 @@ describe('Utils', () => {
1818
expect(s2).toEqual({ counter: 1, stack: [ 'a' ] })
1919
})
2020

21-
it('ignores all props which are not a function', () => {
21+
it('warns on props which are not a function and excludes them', () => {
22+
const preSpy = console.error
23+
const spy = jest.fn()
24+
console.error = spy
25+
2226
const reducer = combineReducers({
2327
fake: true,
2428
broken: 'string',
2529
another: { nested: 'object' },
2630
stack: (state = []) => state
2731
})
2832

33+
expect(spy.mock.calls[0][0]).toMatch(
34+
/Reducer provided for "fake" is not a function/
35+
)
36+
37+
expect(spy.mock.calls[1][0]).toMatch(
38+
/Reducer provided for "broken" is not a function/
39+
)
40+
41+
expect(spy.mock.calls[2][0]).toMatch(
42+
/Reducer provided for "another" is not a function/
43+
)
44+
2945
expect(
3046
Object.keys(reducer({ }, { type: 'push' }))
3147
).toEqual([ 'stack' ])

test/createStore.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import $$observable from 'symbol-observable'
66

77
describe('createStore', () => {
88
it('exposes the public API', () => {
9-
const store = createStore(combineReducers(reducers))
9+
const store = createStore(reducers.todos)
1010
const methods = Object.keys(store)
1111

1212
expect(methods.length).toBe(4)

0 commit comments

Comments
 (0)