-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Prevent error in pretty-format for window in jsdom test env #4750
Changes from 1 commit
28a5a16
0c5e9bb
3fbc2ac
6c00595
620e1c0
c00d3e3
87d89cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,27 @@ const errorToString = Error.prototype.toString; | |
const regExpToString = RegExp.prototype.toString; | ||
const symbolToString = Symbol.prototype.toString; | ||
|
||
// Return whether val is equal to both global and window object. | ||
let noGlobalWindow; | ||
let theGlobalWindow; | ||
const isGlobalWindow = val => { | ||
if (noGlobalWindow) { | ||
return false; | ||
} | ||
if (theGlobalWindow === undefined) { | ||
try { | ||
/* global window */ | ||
noGlobalWindow = global !== window; | ||
if (!noGlobalWindow) { | ||
theGlobalWindow = window; | ||
} | ||
} catch (e) { | ||
noGlobalWindow = true; | ||
} | ||
} | ||
return val === theGlobalWindow; | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just: const isGlobalWindow = val => {
try {
/* global window */
return window === global;
} catch (error) {
return false;
}
}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In answer to your question, must return result of comparison to The complexity is in case some overhead to enclose The more I think about it though, considering There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think I should do a set of perf measurements compared to simplest possible code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was just referring to make it a bit simpler (I now see I forgot to include |
||
const SYMBOL_REGEXP = /^Symbol\((.*)\)(.*)$/; | ||
const NEWLINE_REGEXP = /\n/gi; | ||
|
||
|
@@ -224,7 +245,10 @@ function printComplexValue( | |
'}'; | ||
} | ||
|
||
return hitMaxDepth | ||
// Avoid failure to serialize global window object in jsdom test environment. | ||
// For example, not even relevant if window is prop of React element. | ||
// Theoretically could serialize window in browser if global is undefined. | ||
return hitMaxDepth || isGlobalWindow(val) | ||
? '[' + (val.constructor ? val.constructor.name : 'Object') + ']' | ||
: (min ? '' : (val.constructor ? val.constructor.name : 'Object') + ' ') + | ||
'{' + | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try catch feels like overkill. Why not just
typeof window != 'undefined'
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It catches
ReferenceError: window is not defined
for--env=node
in Jest or ifpretty-format
package is used for other purpose than testing.Keep the questions coming. This one caused me to add comments in the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a new comment - it does not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or well, the current test throws, but that's because the test itself is referring to
window
, not because it throws from inside pretty-format.You can always
typeof
anything - even stuff that is not defined.And this way you avoid the try-catch entirely