Skip to content

Commit b1bcee2

Browse files
committed
Don't depend on console object
Fixes #1311
1 parent 80c7643 commit b1bcee2

File tree

4 files changed

+65
-11
lines changed

4 files changed

+65
-11
lines changed

src/combineReducers.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { ActionTypes } from './createStore'
22
import isPlainObject from './utils/isPlainObject'
33
import mapValues from './utils/mapValues'
44
import pick from './utils/pick'
5-
6-
/* eslint-disable no-console */
5+
import warning from './utils/warning'
76

87
function getUndefinedStateErrorMessage(key, action) {
98
var actionType = action && action.type
@@ -112,7 +111,7 @@ export default function combineReducers(reducers) {
112111
if (process.env.NODE_ENV !== 'production') {
113112
var warningMessage = getUnexpectedStateShapeWarningMessage(state, finalReducers, action)
114113
if (warningMessage) {
115-
console.error(warningMessage)
114+
warning(warningMessage)
116115
}
117116
}
118117

src/index.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import combineReducers from './combineReducers'
33
import bindActionCreators from './bindActionCreators'
44
import applyMiddleware from './applyMiddleware'
55
import compose from './compose'
6+
import warning from './utils/warning'
67

78
/*
89
* This is a dummy function to check if the function name has been altered by minification.
@@ -11,15 +12,13 @@ import compose from './compose'
1112
function isCrushed() {}
1213

1314
if (isCrushed.name !== 'isCrushed' && process.env.NODE_ENV !== 'production') {
14-
/*eslint-disable no-console */
15-
console.error(
16-
'You are currently using minified code outside of NODE_ENV === \'production\'. ' +
17-
'This means that you are running a slower development build of Redux. ' +
18-
'You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify ' +
19-
'or DefinePlugin for webpack (http://stackoverflow.com/questions/30030031) ' +
15+
warning(
16+
'You are currently using minified code outside of NODE_ENV === \'production\'. ' +
17+
'This means that you are running a slower development build of Redux. ' +
18+
'You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify ' +
19+
'or DefinePlugin for webpack (http://stackoverflow.com/questions/30030031) ' +
2020
'to ensure you have the correct code for your production build.'
21-
)
22-
/*eslint-enable */
21+
)
2322
}
2423

2524
export {

src/utils/warning.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Prints a warning in the console if it exists.
3+
*
4+
* @param {String} message The warning message.
5+
* @returns {void}
6+
*/
7+
export default function warning(message) {
8+
/* eslint-disable no-console */
9+
if (typeof console !== 'undefined' && typeof console.error === 'function') {
10+
console.error(message)
11+
}
12+
/* eslint-enable no-console */
13+
try {
14+
// This error was thrown as a convenience so that you can use this stack
15+
// to find the callsite that caused this warning to fire.
16+
throw new Error(message)
17+
/* eslint-disable no-empty */
18+
} catch (e) { }
19+
/* eslint-enable no-empty */
20+
}

test/utils/warning.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import expect from 'expect'
2+
import warning from '../../src/utils/warning'
3+
4+
describe('Utils', () => {
5+
describe('warning', () => {
6+
it('calls console.error when available', () => {
7+
const spy = expect.spyOn(console, 'error')
8+
try {
9+
warning('Test')
10+
expect(spy.calls[0].arguments[0]).toBe('Test')
11+
} finally {
12+
spy.restore()
13+
}
14+
})
15+
16+
it('does not throw when console.error is not available', () => {
17+
const realConsole = global.console
18+
Object.defineProperty(global, 'console', { value: {} })
19+
try {
20+
expect(() => warning('Test')).toNotThrow()
21+
} finally {
22+
Object.defineProperty(global, 'console', { value: realConsole })
23+
}
24+
})
25+
26+
it('does not throw when console is not available', () => {
27+
const realConsole = global.console
28+
Object.defineProperty(global, 'console', { value: undefined })
29+
try {
30+
expect(() => warning('Test')).toNotThrow()
31+
} finally {
32+
Object.defineProperty(global, 'console', { value: realConsole })
33+
}
34+
})
35+
})
36+
})

0 commit comments

Comments
 (0)