Skip to content

Commit 0195835

Browse files
author
Markus Günther
committed
TASK: Use suppressReactErrorLogging to suppress errors in mocha tests
1 parent 790006b commit 0195835

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

src/components/themr.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ const THEMR_CONFIG = typeof Symbol !== 'undefined' ?
2525
Symbol('THEMR_CONFIG') :
2626
'__REACT_CSS_THEMR_CONFIG__'
2727

28+
/**
29+
* Add suppressReactErrorLogging = true to have no errors in the tests.
30+
*
31+
* @param {string} message error message that will be thrown
32+
* @return {void}
33+
*/
34+
const logErrorMessage = message => {
35+
const error = new Error(message)
36+
if (process.env.NODE_ENV === 'TESTING') {
37+
error.suppressReactErrorLogging = true
38+
}
39+
throw error
40+
}
41+
2842
/**
2943
* Themr decorator
3044
* @param {String|Number|Symbol} componentName - Component name
@@ -92,8 +106,11 @@ export default (componentName, localTheme, options = {}) => (ThemedComponent) =>
92106
getNamespacedTheme(props) {
93107
const { themeNamespace, theme } = props
94108
if (!themeNamespace) return theme
95-
if (themeNamespace && !theme) throw new Error('Invalid themeNamespace use in react-css-themr. ' +
109+
110+
if (themeNamespace && !theme) {
111+
logErrorMessage('Invalid themeNamespace use in react-css-themr. ' +
96112
'themeNamespace prop should be used only with theme prop.')
113+
}
97114

98115
return Object.keys(theme)
99116
.filter(key => key.startsWith(themeNamespace))
@@ -205,7 +222,7 @@ function merge(original = {}, mixin = {}) {
205222

206223
default: {
207224
//can't merge an object with a non-object
208-
throw new Error(`You are merging object ${key} with a non-object ${originalValue}`)
225+
logErrorMessage(`You are merging object ${key} with a non-object ${originalValue}`)
209226
}
210227
}
211228
break
@@ -222,7 +239,8 @@ function merge(original = {}, mixin = {}) {
222239
switch (typeof originalValue) {
223240
case 'object': {
224241
//can't merge a non-object with an object
225-
throw new Error(`You are merging non-object ${mixinValue} with an object ${key}`)
242+
logErrorMessage(`You are merging non-object ${mixinValue} with an object ${key}`)
243+
break
226244
}
227245

228246
case 'undefined': {
@@ -261,7 +279,7 @@ function merge(original = {}, mixin = {}) {
261279
*/
262280
function validateComposeOption(composeTheme) {
263281
if ([ COMPOSE_DEEPLY, COMPOSE_SOFTLY, DONT_COMPOSE ].indexOf(composeTheme) === -1) {
264-
throw new Error(
282+
logErrorMessage(
265283
`Invalid composeTheme option for react-css-themr. Valid composition options\
266284
are ${COMPOSE_DEEPLY}, ${COMPOSE_SOFTLY} and ${DONT_COMPOSE}. The given\
267285
option was ${composeTheme}`

test/setup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { jsdom } from 'jsdom'
22

3+
process.env.NODE_ENV = 'TESTING'
34
global.document = jsdom('<!doctype html><html><body></body></html>')
45
global.window = document.defaultView
56
global.navigator = global.window.navigator

0 commit comments

Comments
 (0)