Skip to content
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

Mock Window Location in Tests #12

Merged
merged 2 commits into from Mar 14, 2024
Merged

Conversation

dcpesses
Copy link
Owner

Adds the ability to mock location and history in your unit-tests using mockWindowLocation. (thx @tkrotoff!)

Why?

While we're using react-router-dom, there are some situations where its routing components (e.g. Link, Navigate, etc) can't (or shouldn't) be used. In those scenarios, we need a way to inspect and evaluate any mutations to window.location between tests, especially since we use HashRouter. Otherwise, your tests may fail with an error similar to the following output:

[FAIL] src/api/twitch.spec.ts > TwitchApi > reset > should reset class to initial state
TypeError: Cannot set property hash of [object URL] which has only a getter
 ❯ TwitchApi.reset src/api/twitch/index.js:501:26
    499|     this.resetState();
    500|     this.resetLocalStorageItems();
    501|     window.location.hash = '';
       |                          ^
    502|   };
    503| 
 ❯ src/api/twitch.spec.ts:615:17

Aren't there already simpler ways to stub out the location in tests?

Yes, but with caveats. You could modify the code to use the assign method if it's your own code, but that may not work with any dependencies without monkey patching them. Sometimes you can get away with a function like safelyStubAndThenCleanup or other methods listed on that Jest issue, but I've personally found many of those workarounds to be problematic at best when used with Vitest. mockWindowLocation feels like a more robust and thorough solution that automatically works with both Jest & Vitest without rewriting code.

Copy link

codecov bot commented Mar 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (fa50b70) to head (8698c23).

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #12   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           10        10           
  Lines          496       496           
  Branches        19        19           
=========================================
  Hits           496       496           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dcpesses dcpesses merged commit f4734d0 into main Mar 14, 2024
8 checks passed
@tkrotoff
Copy link

You're welcome, glad my code and code from others help :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants