-
Notifications
You must be signed in to change notification settings - Fork 402
fix(clerk-js): Fix silent failure with setActive org slug #7132
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?
fix(clerk-js): Fix silent failure with setActive org slug #7132
Conversation
|
Cursor Agent can help with this pull request. Just |
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
WalkthroughAdds explicit validation in Clerk.setActive to throw a ClerkRuntimeError when an organization slug is provided but no matching organization exists for the user. Also adds test coverage for the invalid-slug rejection (with a duplicate test instance added). Changes
Sequence DiagramsequenceDiagram
participant Caller
participant Clerk.setActive
participant OrgStore
participant Error
Caller->>Clerk.setActive: setActive({ organization: "invalid-org-slug" })
Clerk.setActive->>OrgStore: lookup organization by slug
OrgStore-->>Clerk.setActive: not found
rect rgb(255, 230, 230)
Note over Clerk.setActive,Error: NEW validation path
Clerk.setActive->>Error: throw ClerkRuntimeError("Unable to find organization with slug \"invalid-org-slug\"...")
Error-->>Caller: reject with error
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Disabled knowledge base sources:
📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
⏰ 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). (28)
Comment |
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.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/clerk-js/src/core/__tests__/clerk.test.ts (1)
381-406: Consider adding edge case coverage for the new validation.The test correctly validates the error path for invalid slugs. To ensure robust coverage and prevent regressions, consider adding tests for:
- Valid behavior with
null- should switch to personal workspace without throwing- Behavior with empty string - currently won't throw due to truthiness check in the implementation
- Behavior with
undefined- should preserve current organizationExample additional test cases:
it('switches to personal workspace when organization is set to null', async () => { const mockSession2 = { id: '1', status, user: { organizationMemberships: [ { id: 'orgmem_id', organization: { id: 'org_id', slug: 'valid-org-slug', }, }, ], }, touch: vi.fn(), getToken: vi.fn(), }; mockClientFetch.mockReturnValue(Promise.resolve({ signedInSessions: [mockSession2] })); const sut = new Clerk(productionPublishableKey); await sut.load(); mockSession2.touch.mockImplementationOnce(() => { sut.session = mockSession2 as any; return Promise.resolve(); }); mockSession2.getToken.mockImplementation(() => 'mocked-token'); // Should not throw, should set to personal workspace await sut.setActive({ organization: null }); await waitFor(() => { expect((mockSession2 as any as ActiveSessionResource)?.lastActiveOrganizationId).toBeNull(); }); }); it('does not throw when organization is set to empty string', async () => { const mockSession2 = { id: '1', status, user: { organizationMemberships: [ { id: 'orgmem_id', organization: { id: 'org_id', slug: 'valid-org-slug', }, }, ], }, touch: vi.fn(), getToken: vi.fn(), }; mockClientFetch.mockReturnValue(Promise.resolve({ signedInSessions: [mockSession2] })); const sut = new Clerk(productionPublishableKey); await sut.load(); mockSession2.touch.mockImplementationOnce(() => { sut.session = mockSession2 as any; return Promise.resolve(); }); mockSession2.getToken.mockImplementation(() => 'mocked-token'); // Empty string is falsy, should switch to personal workspace without throwing await expect(sut.setActive({ organization: '' })).resolves.not.toThrow(); });
📜 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.
📒 Files selected for processing (2)
packages/clerk-js/src/core/__tests__/clerk.test.ts(1 hunks)packages/clerk-js/src/core/clerk.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (9)
**/*.{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/core/__tests__/clerk.test.tspackages/clerk-js/src/core/clerk.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/core/__tests__/clerk.test.tspackages/clerk-js/src/core/clerk.ts
packages/**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
TypeScript is required for all packages
Files:
packages/clerk-js/src/core/__tests__/clerk.test.tspackages/clerk-js/src/core/clerk.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/core/__tests__/clerk.test.tspackages/clerk-js/src/core/clerk.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
Avoidanytype - preferunknownwhen type is uncertain, then narrow with type guards
Useinterfacefor object shapes that might be extended
Usetypefor unions, primitives, and computed types
Preferreadonlyproperties for immutable data structures
Useprivatefor internal implementation details
Useprotectedfor inheritance hierarchies
Usepublicexplicitly for clarity in public APIs
Preferreadonlyfor 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
Useconst assertionsfor literal types:as const
Usesatisfiesoperator 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 ...
Noanytypes without justification
Proper error handling with typed errors
Consistent use ofreadonlyfor 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/core/__tests__/clerk.test.tspackages/clerk-js/src/core/clerk.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/core/__tests__/clerk.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/core/__tests__/clerk.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/core/__tests__/clerk.test.tspackages/clerk-js/src/core/clerk.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/core/__tests__/clerk.test.ts
🧬 Code graph analysis (1)
packages/clerk-js/src/core/__tests__/clerk.test.ts (1)
packages/clerk-js/src/core/clerk.ts (1)
Clerk(197-3012)
Co-authored-by: jeff <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
261ed3d to
022593d
Compare
@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: |
|
Found 18 test failures on Blacksmith runners:
|
This pull request contains changes generated by a Cursor Cloud Agent
Summary by CodeRabbit