-
Notifications
You must be signed in to change notification settings - Fork 8
fix(jest): escape parentheses and brackets in test file paths #417
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(jest): escape parentheses and brackets in test file paths #417
Conversation
Jest interprets CLI arguments as regex patterns. This causes issues with Next.js App Router paths that use parentheses for route groups (e.g., `(main)`) and brackets for dynamic routes (e.g., `[catalogId]`). When paths like `src/app/(main)/test.spec.tsx` are passed to Jest, the `(main)` is interpreted as a regex capture group instead of a literal directory name, causing Jest to fail to match any test files. This fix escapes parentheses and brackets in test file paths before passing them to Jest, converting: - `src/app/(main)/test.tsx` -> `src/app/\(main\)/test.tsx` - `src/app/[id]/test.tsx` -> `src/app/\[id\]/test.tsx` Other regex metacharacters (like dots) are intentionally not escaped because they work fine in practice and escaping them breaks existing behavior. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
Alternative Approach:
|
| Approach | Pros | Cons |
|---|---|---|
| Escaping (this PR) | Works with existing regex behavior, backwards compatible | More complex code, might miss edge cases |
--runTestsByPath |
Simpler, cleaner, better performance per Jest docs | Could be a breaking change if anyone relies on regex matching in paths |
The --runTestsByPath approach is recommended by Jest for CI pipelines. Worth considering as an alternative or future improvement.
|
@jasonwbarnett thanks for this PR, and for logging the related issue 🙇 At first glance it looks like the I need to double check but I believe we will only ever pass file names to the I'll do some experimenting and get back to you. |
|
Hey @malclocke, just wanted to follow up on this. It's been about 3 weeks since your last update - totally understand if the holidays got in the way (we just got back from Christmas and New Years ourselves). Any updates on your experiments with the |
|
Hey @jasonwbarnett! Yeah the team's still mostly on holidays and ramping back up. We'll try look at this this month, but for now you could compile your own bktec with these changes and run with that until we merge it to main? |
|
Hi @jasonwbarnett , I agree that the Also according to Jest documentation, the
Do you want to update the PR with the |
|
Hello 👋 I can pivot to use the |
|
Hello @jasonwbarnett! Yes we are all good. We pass test name as regex when retrying failed test, but this command is configured under a separate config |
Summary
Fixes #416
Problem
Jest interprets CLI arguments as regex patterns. When paths containing
(main)or[catalogId]are passed to Jest, these characters are interpreted as regex metacharacters instead of literal characters, causing Jest to fail to match any test files.This commonly affects Next.js App Router projects which use:
(folderName)for route groups[param]for dynamic route segmentsSolution
Added
escapeJestPathPattern()function that escapes parentheses and brackets in file paths:src/app/(main)/test.tsxbecomessrc/app/\(main\)/test.tsxsrc/app/[id]/test.tsxbecomessrc/app/\[id\]/test.tsxOther regex metacharacters (like dots) are intentionally not escaped because they work fine in practice and escaping them would break existing behavior.
Test plan
TestJestCommandNameAndArgs_WithSpecialCharactersInPathverifying correct escapingTestJestCommandNameAndArgs_*tests continue to pass🤖 Generated with Claude Code