-
Notifications
You must be signed in to change notification settings - Fork 359
fix(shared): Prevent stale closures in useReverification hook #6201
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
base: main
Are you sure you want to change the base?
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@@ -14,7 +14,7 @@ const CLERK_API_REVERIFICATION_ERROR_CODE = 'session_reverification_required'; | |||
async function resolveResult<T>(result: Promise<T> | T): Promise<T | ReturnType<typeof reverificationError>> { | |||
try { | |||
const r = await result; | |||
if (r instanceof Response) { | |||
if (typeof Response !== 'undefined' && r instanceof Response) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Just curious, did this ever run in an env where Response was not defined as a global identifier?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great question! My tests were failing without this, and after digging a bit deeper, I figured that even though we're using jsdom fetch
is not supported and its maintainers have confirmed they won't be using Node's native fetch. This is because they need their own spec-compliant implementation that integrates with jsdom's internals, so the missing Response global is intentional.
Instead of changing the production code to handle this, the best solution was to patch our test environment directly with Node's native fetch APIs (which are stable since v18). This makes the tests pass without altering the hook's logic.
Thanks for the prompt, it definitely led to a better solution 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, nice find. If we have a ticket related to this could you add it on the description of the PR ?
Configures Jest to use a custom test environment that patches the JSDOM global scope with `fetch`, `Request`, and `Response` from Node.js. Since Node.js v18+ provides these fetch-related APIs as stable globals, we can reliably polyfill the test environment to ensure they are always available
📝 WalkthroughWalkthroughA new custom Jest environment, 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (4)
🧰 Additional context used📓 Path-based instructions (6)`**/*.{js,ts,tsx,jsx}`: All code must pass ESLint checks with the project's configuration. Use Prettier for consistent code formatting.
📄 Source: CodeRabbit Inference Engine (.cursor/rules/development.mdc) List of files the instruction was applied to:
`packages/**`: All publishable packages under the @clerk namespace must be located in the packages/ directory.
📄 Source: CodeRabbit Inference Engine (.cursor/rules/global.mdc) List of files the instruction was applied to:
`**/*.{ts,tsx}`: Maintain comprehensive JSDoc comments for public APIs.
📄 Source: CodeRabbit Inference Engine (.cursor/rules/development.mdc) List of files the instruction was applied to:
`**/*.ts`: Always define explicit return types for functions, especially public ...
📄 Source: CodeRabbit Inference Engine (.cursor/rules/typescript.mdc) List of files the instruction was applied to:
`**/*.{test,spec}.{js,ts,tsx,jsx}`: Unit tests are required for all new functionality.
📄 Source: CodeRabbit Inference Engine (.cursor/rules/development.mdc) List of files the instruction was applied to:
`**/__tests__/**/*.{js,ts,tsx,jsx}`: Test files should be co-located with source files or in `__tests__` directories.
📄 Source: CodeRabbit Inference Engine (.cursor/rules/development.mdc) List of files the instruction was applied to:
🧠 Learnings (4)📓 Common learnings
packages/shared/jest.config.js (11)
packages/shared/src/react/__tests__/useReverification.test.ts (11)
packages/shared/src/react/hooks/useReverification.ts (11)
🔇 Additional comments (7)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
@clerk/agent-toolkit
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/dev-cli
@clerk/elements
@clerk/clerk-expo
@clerk/expo-passkeys
@clerk/express
@clerk/fastify
@clerk/localizations
@clerk/nextjs
@clerk/nuxt
@clerk/clerk-react
@clerk/react-router
@clerk/remix
@clerk/shared
@clerk/tanstack-react-start
@clerk/testing
@clerk/themes
@clerk/types
@clerk/upgrade
@clerk/vue
commit: |
Description
Addresses a stale closure bug within the
useReverification
hook and improves the stability of our test environment.The primary issue was that the hook could capture an initial version of the fetcher function and would not use the updated version on subsequent re-renders.
Checklist
pnpm test
runs as expected.pnpm build
runs as expected.Type of change
Summary by CodeRabbit
New Features
Response
API during tests.Bug Fixes
useReverification
hook by updating its internal logic for handler creation and invocation.Tests
useReverification
hook.