From 01958354d7ffbe253fc598da7e59fab47b4c5124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20G=C3=BCnther?= Date: Wed, 1 Aug 2018 22:23:02 +0200 Subject: [PATCH 1/3] TASK: Use suppressReactErrorLogging to suppress errors in mocha tests --- .vscode/settings.json | 2 ++ src/components/themr.js | 26 ++++++++++++++++++++++---- test/setup.js | 1 + 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7a73a41 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/src/components/themr.js b/src/components/themr.js index e19711f..4136c05 100644 --- a/src/components/themr.js +++ b/src/components/themr.js @@ -25,6 +25,20 @@ const THEMR_CONFIG = typeof Symbol !== 'undefined' ? Symbol('THEMR_CONFIG') : '__REACT_CSS_THEMR_CONFIG__' +/** + * Add suppressReactErrorLogging = true to have no errors in the tests. + * + * @param {string} message error message that will be thrown + * @return {void} + */ +const logErrorMessage = message => { + const error = new Error(message) + if (process.env.NODE_ENV === 'TESTING') { + error.suppressReactErrorLogging = true + } + throw error +} + /** * Themr decorator * @param {String|Number|Symbol} componentName - Component name @@ -92,8 +106,11 @@ export default (componentName, localTheme, options = {}) => (ThemedComponent) => getNamespacedTheme(props) { const { themeNamespace, theme } = props if (!themeNamespace) return theme - if (themeNamespace && !theme) throw new Error('Invalid themeNamespace use in react-css-themr. ' + + + if (themeNamespace && !theme) { + logErrorMessage('Invalid themeNamespace use in react-css-themr. ' + 'themeNamespace prop should be used only with theme prop.') + } return Object.keys(theme) .filter(key => key.startsWith(themeNamespace)) @@ -205,7 +222,7 @@ function merge(original = {}, mixin = {}) { default: { //can't merge an object with a non-object - throw new Error(`You are merging object ${key} with a non-object ${originalValue}`) + logErrorMessage(`You are merging object ${key} with a non-object ${originalValue}`) } } break @@ -222,7 +239,8 @@ function merge(original = {}, mixin = {}) { switch (typeof originalValue) { case 'object': { //can't merge a non-object with an object - throw new Error(`You are merging non-object ${mixinValue} with an object ${key}`) + logErrorMessage(`You are merging non-object ${mixinValue} with an object ${key}`) + break } case 'undefined': { @@ -261,7 +279,7 @@ function merge(original = {}, mixin = {}) { */ function validateComposeOption(composeTheme) { if ([ COMPOSE_DEEPLY, COMPOSE_SOFTLY, DONT_COMPOSE ].indexOf(composeTheme) === -1) { - throw new Error( + logErrorMessage( `Invalid composeTheme option for react-css-themr. Valid composition options\ are ${COMPOSE_DEEPLY}, ${COMPOSE_SOFTLY} and ${DONT_COMPOSE}. The given\ option was ${composeTheme}` diff --git a/test/setup.js b/test/setup.js index c2e0f0c..12f9061 100644 --- a/test/setup.js +++ b/test/setup.js @@ -1,5 +1,6 @@ import { jsdom } from 'jsdom' +process.env.NODE_ENV = 'TESTING' global.document = jsdom('') global.window = document.defaultView global.navigator = global.window.navigator From fef61dfa19a05a016c6edb0009166e96d31faf35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20G=C3=BCnther?= Date: Thu, 2 Aug 2018 09:36:53 +0200 Subject: [PATCH 2/3] TASK: Use polyfill to suppress ReactAnimationFrame warning --- test/setup.js | 2 +- test/tempPolyfills.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 test/tempPolyfills.js diff --git a/test/setup.js b/test/setup.js index 12f9061..d890537 100644 --- a/test/setup.js +++ b/test/setup.js @@ -1,6 +1,6 @@ +import raf from './tempPolyfills' import { jsdom } from 'jsdom' -process.env.NODE_ENV = 'TESTING' global.document = jsdom('') global.window = document.defaultView global.navigator = global.window.navigator diff --git a/test/tempPolyfills.js b/test/tempPolyfills.js new file mode 100644 index 0000000..7110b0a --- /dev/null +++ b/test/tempPolyfills.js @@ -0,0 +1,5 @@ +const raf = global.requestAnimationFrame = (cb) => { + setTimeout(cb, 0) +} + +export default raf From 024695612005e7129922563155e4f211adde3602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20G=C3=BCnther?= Date: Thu, 2 Aug 2018 09:37:59 +0200 Subject: [PATCH 3/3] TASK: Disable console.error in tests instead of suppressReactErrorLogging --- src/components/themr.js | 22 ++++------------------ test/components/ThemeProvider.spec.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/components/themr.js b/src/components/themr.js index 4136c05..285fcae 100644 --- a/src/components/themr.js +++ b/src/components/themr.js @@ -25,20 +25,6 @@ const THEMR_CONFIG = typeof Symbol !== 'undefined' ? Symbol('THEMR_CONFIG') : '__REACT_CSS_THEMR_CONFIG__' -/** - * Add suppressReactErrorLogging = true to have no errors in the tests. - * - * @param {string} message error message that will be thrown - * @return {void} - */ -const logErrorMessage = message => { - const error = new Error(message) - if (process.env.NODE_ENV === 'TESTING') { - error.suppressReactErrorLogging = true - } - throw error -} - /** * Themr decorator * @param {String|Number|Symbol} componentName - Component name @@ -108,7 +94,7 @@ export default (componentName, localTheme, options = {}) => (ThemedComponent) => if (!themeNamespace) return theme if (themeNamespace && !theme) { - logErrorMessage('Invalid themeNamespace use in react-css-themr. ' + + throw new Error('Invalid themeNamespace use in react-css-themr. ' + 'themeNamespace prop should be used only with theme prop.') } @@ -222,7 +208,7 @@ function merge(original = {}, mixin = {}) { default: { //can't merge an object with a non-object - logErrorMessage(`You are merging object ${key} with a non-object ${originalValue}`) + throw new Error(`You are merging object ${key} with a non-object ${originalValue}`) } } break @@ -239,7 +225,7 @@ function merge(original = {}, mixin = {}) { switch (typeof originalValue) { case 'object': { //can't merge a non-object with an object - logErrorMessage(`You are merging non-object ${mixinValue} with an object ${key}`) + throw new Error(`You are merging non-object ${mixinValue} with an object ${key}`) break } @@ -279,7 +265,7 @@ function merge(original = {}, mixin = {}) { */ function validateComposeOption(composeTheme) { if ([ COMPOSE_DEEPLY, COMPOSE_SOFTLY, DONT_COMPOSE ].indexOf(composeTheme) === -1) { - logErrorMessage( + throw new Error( `Invalid composeTheme option for react-css-themr. Valid composition options\ are ${COMPOSE_DEEPLY}, ${COMPOSE_SOFTLY} and ${DONT_COMPOSE}. The given\ option was ${composeTheme}` diff --git a/test/components/ThemeProvider.spec.js b/test/components/ThemeProvider.spec.js index 478400e..da4df5c 100644 --- a/test/components/ThemeProvider.spec.js +++ b/test/components/ThemeProvider.spec.js @@ -4,6 +4,16 @@ import PropTypes from 'prop-types' import TestUtils from 'react-dom/test-utils' import { ThemeProvider } from '../../src/index' + +before(function () { + /* eslint-disable no-console */ + console.error = function () {} +}) + +after(function () { + delete console.error +}) + describe('ThemeProvider', () => { class Child extends Component { render() {