-
Notifications
You must be signed in to change notification settings - Fork 402
fix(e2e): Align middleware detection test cases for next@16 #7047
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
Conversation
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
WalkthroughThe test file introduces utility functions to detect Next.js version and dynamically adjust test execution. Tests conditionally skip or modify assertions based on Next.js major version, with specific handling for Next 16+ compatibility. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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 (2)
integration/tests/middleware-placement.test.ts (2)
10-16: Consider adding JSDoc and potentially more robust semver parsing.While the current regex
/\d+/works for common cases, it's quite simplistic. For a test helper, this is acceptable, but consider:
- Adding a JSDoc comment to clarify the function's purpose and limitations.
- If you need more robust semver parsing in the future, consider using a dedicated library like
semver.The current implementation is sufficient for detecting major versions from typical npm version ranges.
91-93: Optional: Consider extracting version detection to reduce duplication.Both tests in this describe block call
detectNext(app)and parse the major version. You could extract this to a variable in thebeforeAllhook for slight optimization and DRY principle:let nextMajorVersion: number; test.beforeAll(async () => { // ... existing setup ... const { version } = await detectNext(app); nextMajorVersion = parseSemverMajor(version) ?? 0; });However, the current approach is more explicit and easier to read in test code, so this is purely optional.
Also applies to: 103-110
📜 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 (1)
integration/tests/middleware-placement.test.ts(3 hunks)
🧰 Additional context used
📓 Path-based instructions (7)
**/*.{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:
integration/tests/middleware-placement.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:
integration/tests/middleware-placement.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
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:
integration/tests/middleware-placement.test.ts
integration/**
📄 CodeRabbit inference engine (.cursor/rules/global.mdc)
Framework integration templates and E2E tests should be placed under the integration/ directory
Files:
integration/tests/middleware-placement.test.ts
integration/**/*
📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)
End-to-end tests and integration templates must be located in the 'integration/' directory.
Files:
integration/tests/middleware-placement.test.ts
integration/**/*.{test,spec}.{js,ts}
📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)
Integration tests should use Playwright.
Files:
integration/tests/middleware-placement.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:
integration/tests/middleware-placement.test.ts
🧬 Code graph analysis (1)
integration/tests/middleware-placement.test.ts (3)
integration/models/application.ts (1)
Application(7-7)integration/models/stateFile.ts (1)
stateFile(103-103)integration/testUtils/index.ts (1)
createTestUtils(24-86)
⏰ 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). (3)
- GitHub Check: Analyze (javascript-typescript)
- GitHub Check: semgrep-cloud-platform/scan
- GitHub Check: semgrep-cloud-platform/scan
🔇 Additional comments (3)
integration/tests/middleware-placement.test.ts (3)
2-2: LGTM!The new imports are necessary for the Next.js version detection helpers.
Also applies to: 5-5, 7-7
91-93: Good approach to version-specific test skipping.The conditional skip logic correctly handles Next.js version differences. The
?? 0fallback ensures that if the version cannot be detected, the test runs (assumes pre-16 behavior).
103-110: LGTM! Complementary test case for Next 16+ behavior.This test correctly validates that Next.js 16+ does not show the misplaced middleware error, complementing the pre-16 test above. The conditional skip and assertion logic are sound.
| async function detectNext(app: Application): Promise<{ isNext: boolean; version?: string }> { | ||
| // app.appDir exists for normal Application; for long-running apps, read it from the state file by serverUrl | ||
| const appDir = | ||
| (app as any).appDir || | ||
| Object.values(stateFile.getLongRunningApps() || {}).find(a => a.serverUrl === app.serverUrl)?.appDir; | ||
|
|
||
| if (!appDir) { | ||
| return { isNext: false }; | ||
| } | ||
|
|
||
| const pkg = await fs.readJSON(path.join(appDir, 'package.json')); | ||
| const nextRange: string | undefined = pkg.dependencies?.next || pkg.devDependencies?.next; | ||
|
|
||
| return { isNext: Boolean(nextRange), version: nextRange }; | ||
| } |
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.
Add error handling and avoid any type cast.
The function has two concerns:
-
Line 21: Using
(app as any).appDirviolates the coding guideline to avoidanytypes. Consider:- Adding
appDirto theApplicationtype if it's a valid property - Using a type guard or checking if the property exists with proper typing
- Using
as unknown as { appDir?: string }as a safer intermediate cast
- Adding
-
Line 28:
fs.readJSON()has no error handling. Ifpackage.jsonis missing or malformed, the test will fail with an unclear error. Consider wrapping in try-catch and returning{ isNext: false }on error.
Apply this diff to improve error handling:
async function detectNext(app: Application): Promise<{ isNext: boolean; version?: string }> {
// app.appDir exists for normal Application; for long-running apps, read it from the state file by serverUrl
const appDir =
(app as any).appDir ||
Object.values(stateFile.getLongRunningApps() || {}).find(a => a.serverUrl === app.serverUrl)?.appDir;
if (!appDir) {
return { isNext: false };
}
+ try {
const pkg = await fs.readJSON(path.join(appDir, 'package.json'));
const nextRange: string | undefined = pkg.dependencies?.next || pkg.devDependencies?.next;
return { isNext: Boolean(nextRange), version: nextRange };
+ } catch {
+ return { isNext: false };
+ }
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| async function detectNext(app: Application): Promise<{ isNext: boolean; version?: string }> { | |
| // app.appDir exists for normal Application; for long-running apps, read it from the state file by serverUrl | |
| const appDir = | |
| (app as any).appDir || | |
| Object.values(stateFile.getLongRunningApps() || {}).find(a => a.serverUrl === app.serverUrl)?.appDir; | |
| if (!appDir) { | |
| return { isNext: false }; | |
| } | |
| const pkg = await fs.readJSON(path.join(appDir, 'package.json')); | |
| const nextRange: string | undefined = pkg.dependencies?.next || pkg.devDependencies?.next; | |
| return { isNext: Boolean(nextRange), version: nextRange }; | |
| } | |
| async function detectNext(app: Application): Promise<{ isNext: boolean; version?: string }> { | |
| // app.appDir exists for normal Application; for long-running apps, read it from the state file by serverUrl | |
| const appDir = | |
| (app as any).appDir || | |
| Object.values(stateFile.getLongRunningApps() || {}).find(a => a.serverUrl === app.serverUrl)?.appDir; | |
| if (!appDir) { | |
| return { isNext: false }; | |
| } | |
| try { | |
| const pkg = await fs.readJSON(path.join(appDir, 'package.json')); | |
| const nextRange: string | undefined = pkg.dependencies?.next || pkg.devDependencies?.next; | |
| return { isNext: Boolean(nextRange), version: nextRange }; | |
| } catch { | |
| return { isNext: false }; | |
| } | |
| } |
🤖 Prompt for AI Agents
In integration/tests/middleware-placement.test.ts around lines 18 to 32, avoid
the unsafe (app as any).appDir cast and add error handling for fs.readJSON():
replace the any cast by either extending the Application type to include
optional appDir or use a narrow type-guard/safer intermediate cast (e.g. as
unknown as { appDir?: string }) before reading appDir, then wrap the
fs.readJSON(path.join(appDir, 'package.json')) call in a try-catch and return {
isNext: false } if reading/parsing fails so missing or malformed package.json
doesn’t crash the test.
@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
fixes nightly e2e tests. This seems to be working due to turbopack now being utilized by default.
Checklist
pnpm testruns as expected.pnpm buildruns as expected.Type of change
Summary by CodeRabbit