Skip to content

Conversation

@jacekradko
Copy link
Member

@jacekradko jacekradko commented Nov 5, 2025

Description

Adding base test coverage for memoizeStateListenerCallback. Primarily added this when verifying we are handling the null to undefined and vice-versa scenarios

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Summary by CodeRabbit

  • Tests
    • Expanded test coverage for state-listener memoization, adding comprehensive edge-case validation including null vs. undefined transitions and repeated-call behavior.
  • Chores
    • Added a changeset placeholder file (no runtime or API changes).

@changeset-bot
Copy link

changeset-bot bot commented Nov 5, 2025

🦋 Changeset detected

Latest commit: d517cc5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Nov 5, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
clerk-js-sandbox Ready Ready Preview Comment Nov 6, 2025 2:57pm

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 5, 2025

Walkthrough

Added a new changeset file and replaced placeholder tests with a comprehensive test suite for memoizeStateListenerCallback covering memoization, id/updated_at changes, null/undefined transitions, and callback invocation (≤50 words).

Changes

Cohort / File(s) Summary
Changeset metadata
/.changeset/slimy-maps-read.md
New changeset file added containing metadata separators; no code changes.
Test suite expansion
packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts
Replaced skipped stubs with full tests validating memoization behavior: stable references when unchanged, new references when updated_at or id change, transitions to/from null and undefined, and ensuring listener callback is invoked each call.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Inspect test assertions for correctness against memoization rules
  • Verify Vi spy usage and mock/test utilities (createTestUser)
  • Check explicit null vs undefined handling and relevant edge cases

Poem

🐰 A test hop, a changeset gleam,
New checks awake from a skipped-test dream.
IDs and timestamps now get their say,
Nulls and undefined won't hide away.
Hooray for coverage — onward we play! 🎉

Pre-merge checks and finishing touches

✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding test coverage for the memoizeStateListenerCallback function in clerk-js.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch chore/add-test-coverage

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 5, 2025

Open in StackBlitz

@clerk/agent-toolkit

npm i https://pkg.pr.new/@clerk/agent-toolkit@7152

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@7152

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@7152

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@7152

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@7152

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@7152

@clerk/elements

npm i https://pkg.pr.new/@clerk/elements@7152

@clerk/clerk-expo

npm i https://pkg.pr.new/@clerk/clerk-expo@7152

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@7152

@clerk/express

npm i https://pkg.pr.new/@clerk/express@7152

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@7152

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@7152

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@7152

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@7152

@clerk/clerk-react

npm i https://pkg.pr.new/@clerk/clerk-react@7152

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@7152

@clerk/remix

npm i https://pkg.pr.new/@clerk/remix@7152

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@7152

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@7152

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@7152

@clerk/themes

npm i https://pkg.pr.new/@clerk/themes@7152

@clerk/types

npm i https://pkg.pr.new/@clerk/types@7152

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@7152

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@7152

commit: d517cc5

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (3)
packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts (3)

9-17: Consider addressing the type assertion to improve type safety.

The as unknown as UserJSON assertion bypasses TypeScript's type checking. While this may be intentional for test simplification, it could mask type incompatibilities.

Consider either:

  1. Providing complete UserJSON data to satisfy the type without assertion
  2. Creating a typed test fixture that properly implements UserJSON
  3. Using a type-safe mock library if the full UserJSON structure is too verbose

As per coding guidelines: "Avoid any type - prefer unknown when type is uncertain, then narrow with type guards."


29-34: Consider extracting the callback tracking pattern.

The pattern of tracking calledWith via a closure is repeated across multiple tests. While functional, extracting this into a helper could improve maintainability.

Example helper:

function createTrackingListener() {
  let calledWith: Resources | undefined;
  const listener = memoizeListenerCallback(
    vi.fn((e: Resources) => {
      calledWith = e;
    })
  );
  return { listener, getCalledWith: () => calledWith };
}

Usage:

const { listener, getCalledWith } = createTrackingListener();
listener({ client: null, organization: null, session: null, user: user1 });
expect(getCalledWith()?.user).toBe(user1);

7-205: Consider adding test coverage for other resources.

The test suite focuses exclusively on the user resource, but the memoizeListenerCallback function handles all resources (client, organization, session, user). While user testing is valuable, verifying memoization behavior for other resources would provide more comprehensive coverage.

Consider adding tests like:

it('returns same client ref if client state has not changed', () => {
  // Similar pattern to user tests but for client resource
});

it('returns new session ref if session.id has changed', () => {
  // Test session memoization
});
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 538bf81 and 789f5d3.

📒 Files selected for processing (2)
  • .changeset/slimy-maps-read.md (1 hunks)
  • packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (10)
.changeset/**

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Automated releases must use Changesets.

Files:

  • .changeset/slimy-maps-read.md
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

**/*.{js,jsx,ts,tsx}: All code must pass ESLint checks with the project's configuration
Follow established naming conventions (PascalCase for components, camelCase for variables)
Maintain comprehensive JSDoc comments for public APIs
Use dynamic imports for optional features
All public APIs must be documented with JSDoc
Provide meaningful error messages to developers
Include error recovery suggestions where applicable
Log errors appropriately for debugging
Lazy load components and features when possible
Implement proper caching strategies
Use efficient data structures and algorithms
Profile and optimize critical paths
Validate all inputs and sanitize outputs
Implement proper logging with different levels

Files:

  • packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts
**/*.{js,jsx,ts,tsx,json,css,scss,md,yaml,yml}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Use Prettier for consistent code formatting

Files:

  • packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts
packages/**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

TypeScript is required for all packages

Files:

  • packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts
packages/**/*.{ts,tsx,d.ts}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Packages should export TypeScript types alongside runtime code

Files:

  • packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Use proper TypeScript error types

**/*.{ts,tsx}: Always define explicit return types for functions, especially public APIs
Use proper type annotations for variables and parameters where inference isn't clear
Avoid any type - prefer unknown when type is uncertain, then narrow with type guards
Use interface for object shapes that might be extended
Use type for unions, primitives, and computed types
Prefer readonly properties for immutable data structures
Use private for internal implementation details
Use protected for inheritance hierarchies
Use public explicitly for clarity in public APIs
Prefer readonly for properties that shouldn't change after construction
Prefer composition and interfaces over deep inheritance chains
Use mixins for shared behavior across unrelated classes
Implement dependency injection for loose coupling
Let TypeScript infer when types are obvious
Use const assertions for literal types: as const
Use satisfies operator for type checking without widening
Use mapped types for transforming object types
Use conditional types for type-level logic
Leverage template literal types for string manipulation
Use ES6 imports/exports consistently
Use default exports sparingly, prefer named exports
Use type-only imports: import type { ... } from ...
No any types without justification
Proper error handling with typed errors
Consistent use of readonly for immutable data
Proper generic constraints
No unused type parameters
Proper use of utility types instead of manual type construction
Type-only imports where possible
Proper tree-shaking friendly exports
No circular dependencies
Efficient type computations (avoid deep recursion)

Files:

  • packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts
packages/**/*.{test,spec}.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Unit tests should use Jest or Vitest as the test runner.

Files:

  • packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts
packages/{clerk-js,elements,themes}/**/*.{test,spec}.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Visual regression testing should be performed for UI components.

Files:

  • packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts
**/*.{js,ts,tsx,jsx}

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Support multiple Clerk environment variables (CLERK_, NEXT_PUBLIC_CLERK_, etc.) for configuration.

Files:

  • packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts
**/__tests__/**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/typescript.mdc)

**/__tests__/**/*.{ts,tsx}: Create type-safe test builders/factories
Use branded types for test isolation
Implement proper mock types that match interfaces

Files:

  • packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts
🧬 Code graph analysis (1)
packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts (1)
packages/clerk-js/src/utils/memoizeStateListenerCallback.ts (1)
  • memoizeListenerCallback (139-146)
🪛 LanguageTool
.changeset/slimy-maps-read.md

[grammar] ~1-~1: Hier könnte ein Fehler sein.
Context: --- ---

(QB_NEW_DE)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: Formatting | Dedupe | Changeset
  • GitHub Check: Build Packages
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: semgrep-cloud-platform/scan
  • GitHub Check: semgrep-cloud-platform/scan
🔇 Additional comments (3)
packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts (3)

1-5: LGTM!

Imports are well-structured with proper type-only imports and named exports.


158-176: Excellent callback invocation test.

This test correctly verifies that the callback is invoked on each listener call, which is important for ensuring the memoization doesn't prevent notification delivery.


178-204: Great edge case coverage for null vs undefined.

These tests correctly verify that the memoization logic distinguishes between null and undefined, which is crucial for proper state management in JavaScript/TypeScript.

@blacksmith-sh

This comment has been minimized.

@blacksmith-sh

This comment has been minimized.

@jacekradko jacekradko merged commit 4fc9240 into main Nov 6, 2025
42 checks passed
@jacekradko jacekradko deleted the chore/add-test-coverage branch November 6, 2025 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants