-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
window.history cannot be cleared, push "/" URL instead
- Loading branch information
Showing
2 changed files
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
console.log('setup-after-env.ts'); | ||
Check failure on line 1 in jest.setup-after-env.ts GitHub Actions / build (18.x)
|
||
|
||
beforeEach(() => { | ||
// window.history cannot be cleared: https://stackoverflow.com/q/20044554 | ||
// | ||
// So BrowserRouter cannot be cleared between tests: | ||
// - https://github.com/testing-library/react-testing-library/issues/518 | ||
// - https://v5.reactrouter.com/web/guides/testing | ||
// "You can also use BrowserRouter if your test environment has the browser globals window.location and window.history | ||
// (which is the default in Jest through JSDOM, but **you cannot reset the history between tests**)" | ||
// | ||
// The only solution is to push a new state to the history with "/" URL between tests | ||
// so window.location.href is not polluted by previous tests in the same file | ||
// | ||
// Other solutions: | ||
// - Use MemoryRouter instead of BrowserRouter when possible | ||
// - Move each test that uses BrowserRouter in its own file | ||
window.history.pushState({}, 'unused', '/'); | ||
}); |