Skip to content
This repository was archived by the owner on Nov 3, 2025. It is now read-only.

fix: two failing tests for React 19 RC - #40

Open
tumugin (tumugin) wants to merge 2 commits into
microsoft:mainfrom
tumugin:fixed-react-19-issues
Open

tumugin (tumugin) wants to merge 2 commits into
microsoft:mainfrom
tumugin:fixed-react-19-issues

Conversation

@tumugin

Copy link
Copy Markdown
Contributor

During testing with React 19 RC, I discovered that several tests for this library were failing.

This pull request addresses these failures by modifying the useStrictMemo hook to align with the React.StrictMode behavior of useMemo in React 19 RC.

To maintain compatibility with earlier React versions, I've included some minor workarounds.


React 19 RC changes: https://react.dev/blog/2024/04/25/react-19-upgrade-guide#strict-mode-improvements

@tumugin tumugin (tumugin) changed the title Fixed react 19 issues fix: two failing tests for React 19 RC Oct 25, 2024
@tumugin

Copy link
Copy Markdown
Contributor Author

Test results for React 19 RC

 ✓ src/useDisposable.test.ts (8)
   ✓ useDisposable (8)
     ✓ in strict mode (4)
       ✓ should call factory once during mount
       ✓ should not call dispose on mount
       ✓ should call dispose on unmount
       ✓ should call dispose and call factory if dependencies update
     ✓ not strict mode (4)
       ✓ should call factory once during mount
       ✓ should not call dispose on mount
       ✓ should call dispose on unmount
       ✓ should call dispose and call factory if dependencies update
 ✓ src/useIsStrictMode.test.ts (3)
   ✓ useIsStrictMode (3)
     ✓ should return `true` if wrapped with StrictMode
     ✓ should return `false` if not wrapped with StrictMode
     ✓ should return `false` always in production mode
 ✓ src/useStrictEffect.test.ts (2)
   ✓ useStrictEffect (2)
     ✓ should only dispose on unmount in strict mode
     ✓ should dispose when dependency array changes
 ✓ src/useStrictMemo.test.ts (2)
   ✓ useStrictMemo (2)
     ✓ should not call factory twice on mount in strict mode
     ✓ should call factory if dependencies update

 Test Files  4 passed (4)
      Tests  15 passed (15)
   Start at  05:26:17
   Duration  1.63s (transform 126ms, setup 0ms, collect 1.90s, tests 51ms, environment 2.32s, prepare 1.42s)

Test results for React 18

✓ src/useDisposable.test.ts (8)
   ✓ useDisposable (8)
     ✓ in strict mode (4)
       ✓ should call factory once during mount
       ✓ should not call dispose on mount
       ✓ should call dispose on unmount
       ✓ should call dispose and call factory if dependencies update
     ✓ not strict mode (4)
       ✓ should call factory once during mount
       ✓ should not call dispose on mount
       ✓ should call dispose on unmount
       ✓ should call dispose and call factory if dependencies update
 ✓ src/useIsStrictMode.test.ts (3)
   ✓ useIsStrictMode (3)
     ✓ should return `true` if wrapped with StrictMode
     ✓ should return `false` if not wrapped with StrictMode
     ✓ should return `false` always in production mode
 ✓ src/useStrictEffect.test.ts (2)
   ✓ useStrictEffect (2)
     ✓ should only dispose on unmount in strict mode
     ✓ should dispose when dependency array changes
 ✓ src/useStrictMemo.test.ts (2)
   ✓ useStrictMemo (2)
     ✓ should not call factory twice on mount in strict mode
     ✓ should call factory if dependencies update

 Test Files  4 passed (4)
      Tests  15 passed (15)
   Start at  05:27:24
   Duration  1.22s (transform 131ms, setup 0ms, collect 965ms, tests 44ms, environment 2.66s, prepare 285ms)

@layershifter

Oleksandr Fediashov (layershifter) commented Oct 25, 2024 •

Copy link
Copy Markdown
Contributor

tumugin (@tumugin) thanks for contributing ❤️


Let me provide some context around. We were looking together with ling1726 today into it as I found out that the library does not behave well even with React 18... 😟


Here is a quick demo: https://stackblitz.com/edit/vitejs-vite-kd69zt

Try to use increment button, you will notice the following:

image

The issue is that fiber reference that we are using via getCurrentOwner() is not stable as we thought 💥

We also noticed that its-fine now uses React tree traversal to identify a fiber (https://github.com/pmndrs/its-fine/blob/598b81f02778c22ed21121c2b1a786bdefb14e23/src/index.tsx#L105-L125), but I won't be helpful for us as useId() also is changing between renders ☹️ I.e. we can't use a fiber as an identifier 🥹

@tumugin

Copy link
Copy Markdown
Contributor Author

Oleksandr Fediashov (@layershifter) Thanks for the information about the context. This sounds like a difficult problem to solve 😭😭😭😭

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants