-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Prevent error in pretty-format for window in jsdom test env #4750
Changes from 3 commits
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,26 @@ const errorToString = Error.prototype.toString; | |
const regExpToString = RegExp.prototype.toString; | ||
const symbolToString = Symbol.prototype.toString; | ||
|
||
// Return whether val is equal to global window object. | ||
let noWindow; | ||
let theWindow; | ||
const isWindow = val => { | ||
if (noWindow) { | ||
return false; | ||
} | ||
if (theWindow === undefined) { | ||
try { | ||
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. Try catch feels like overkill. Why not just 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. It catches 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 commentThe reason will be displayed to describe this comment to others. Learn more. I added a new comment - it does not. $ node -p 'typeof window'
undefined 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. Or well, the current test throws, but that's because the test itself is referring to You can always And this way you avoid the try-catch entirely |
||
/* global window */ | ||
theWindow = window; | ||
noWindow = false; | ||
} catch (e) { | ||
noWindow = true; | ||
return false; | ||
} | ||
} | ||
return val === theWindow; | ||
}; | ||
|
||
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 +244,9 @@ 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. | ||
return hitMaxDepth || isWindow(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.
I still don't think this is necessary. This change passes your test as well:
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 suppose you can cache the
typeof
check, but that doesn't seem like an optimizationThere 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.
Yes indeed,
typeof window
works in my experimental codes wherewindow
throws.Thank you very much, I will update pull request according to your one liner above.