Verify window exists before resetting clipboard stub #1176
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:
Ensure that
globalThis.window
exists before resetting/detaching the clipboard stub in theafterEach
/afterAll
hooks.Why:
Using this library in a test suite does not imply that every test in the suite will run in a browser-like environment. I have a test suite that initializes
jsdom
to test client-side code, but then tears it down after the tests. I wanted to use this library, but I couldn't because as soon as a test ran that didn't initializejsdom
, thisafterEach
hook would run and throw an error becauseglobalThis.window
was undefined.How:
Verify that
globalThis.window
exists inside of the hook. I didn't want to put the check inside ofresetClipboardStubOnView
because this file is meant to be run in a browser-like environment, so it seemed most appropriate to put the check inside the hook.Checklist:
There were no existing tests related to the
afterEach
/afterAll
hooks, and I wasn't sure how to go about writing a test for those, so this code does not have any unit tests around it.