Skip to content

Warns when type of reducers passed is invalid to avoid silent failure #2636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/combineReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export default function combineReducers(reducers) {
if (process.env.NODE_ENV !== 'production') {
if (typeof reducers[key] === 'undefined') {
warning(`No reducer provided for key "${key}"`)
} else if (key === '__esModule') {
warning(`Passing a whole ES Module to combine reducers is discouraged.`)
} else if (typeof reducers[key] !== 'function') {
warning(`Reducer provided for "${key}" is not a function. Received type: ${typeof key}.`)
}
}

Expand Down
33 changes: 26 additions & 7 deletions test/combineReducers.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-console */
import { combineReducers } from '../src'
import createStore, { ActionTypes } from '../src/createStore'
import * as reducers from './helpers/reducers'

describe('Utils', () => {
describe('combineReducers', () => {
Expand All @@ -18,17 +19,39 @@ describe('Utils', () => {
expect(s2).toEqual({ counter: 1, stack: [ 'a' ] })
})

it('ignores all props which are not a function', () => {
it('warns on props which are not a function and excludes them', () => {
const preSpy = console.error
const spy = jest.fn()
console.error = spy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've overridden console.error, but there's no mechanism here to put it back after the test.

The Jest way to do this is to make a mock of utils/warning to capture any calls to warning().

jest.mock('../src/helpers/warning')

https://facebook.github.io/jest/docs/en/manual-mocks.html has more info :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can do what we do in other tests. Just add this at the end:

       spy.mockClear()
       console.error = preSpy


const reducer = combineReducers({
fake: true,
broken: 'string',
another: { nested: 'object' },
stack: (state = []) => state
})

expect(spy).toHaveBeenLastCalledWith('Reducer provided for "another" is not a function. Received type: string.')
expect(spy).toHaveBeenCalledTimes(3)

expect(
Object.keys(reducer({ }, { type: 'push' }))
).toEqual([ 'stack' ])

spy.mockClear()
console.error = preSpy
})

it('warns if a module imported with * syntax is passed', () => {
const preSpy = console.error
const spy = jest.fn()
console.error = spy

combineReducers(reducers)
expect(spy).toHaveBeenCalledWith(`Passing a whole ES Module to combine reducers is discouraged.`)

spy.mockClear()
console.error = preSpy
})

it('warns if a reducer prop is undefined', () => {
Expand All @@ -38,15 +61,11 @@ describe('Utils', () => {

let isNotDefined
combineReducers({ isNotDefined })
expect(spy.mock.calls[0][0]).toMatch(
/No reducer provided for key "isNotDefined"/
)
expect(spy).toHaveBeenCalledWith('No reducer provided for key "isNotDefined"')

spy.mockClear()
combineReducers({ thing: undefined })
expect(spy.mock.calls[0][0]).toMatch(
/No reducer provided for key "thing"/
)
expect(spy).toHaveBeenCalledWith('No reducer provided for key "thing"')

spy.mockClear()
console.error = preSpy
Expand Down
2 changes: 1 addition & 1 deletion test/createStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import $$observable from 'symbol-observable'

describe('createStore', () => {
it('exposes the public API', () => {
const store = createStore(combineReducers(reducers))
const store = createStore(reducers.todos)
const methods = Object.keys(store)

expect(methods.length).toBe(4)
Expand Down