fix: remove implicit global references #932
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What:
Remove global references or make them explicit.
Why:
There might be problems with shadowed global variables in testing environments. See #892
The way e.g. jest hoists code we cannot be sure that a reference to e.g.
window
in a module is a reference toglobalThis.window
. We further cannot be sure that e.g.document.defaultView
references the same object aswindow
.Although this is probably no issue in most cases we should be explicit about where we access objects through variables the user provided per
setup()
and where we access through global variables.#929 , #931 enforce to be explicit about access to globals. Any reference that can't be found per text search for
globalThis.
results in a linting error.How:
Clipboard
util. We already pass downwindow
to thecreate
functions so we can use the scoped variable in theclass
.We still access the global
window
for ourafterEach
andafterAll
hooks toreset
/detach
theClipboard
stub. This is probably not an issue as the environments providing a globalafter*
hook are probably identical to environments mergingwindow
into the global scope (like jest+jsdom).If you happen to use an environment where you need to work around this, please leave a comment.
setup()
explicit. If the user provides adocument
withdefaultView
, these won't be used.parseInt
,setTimeout
) in utils explicit.Checklist: