fix: add monorepo root node_modules to Jest moduleDirectories#1900
Conversation
In monorepo setups (e.g., Strapi), workspace packages are hoisted to the root node_modules. When Jest runs from a subpackage, it can't resolve these packages because its default moduleDirectories only includes the local node_modules. This adds the monorepo root's node_modules to moduleDirectories in the runtime Jest config. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Claude finished @mohammedahmed18's task in 2m 26s —— View job PR Review SummaryChecklist
Prek Checks
Code ReviewCorrectness: ✅ Logic is sound The implementation correctly:
One bug: When Minor concern: path injection risk
module_dirs_line = f" moduleDirectories: [...(baseConfig.moduleDirectories || ['node_modules']), '{monorepo_node_modules}'],\n"If the path contains a single quote (e.g.
Duplicate DetectionNo duplicates. Test CoverageTests cover: yarn workspace detection, standalone (returns None), monorepo config with and without base config. Coverage is appropriate for the change. Last updated: 2026-03-25T17:08Z |
Summary
node_modules. When Jest runs from a subpackage, it can't resolve these packages because its defaultmoduleDirectoriesonly includes the localnode_modules._find_monorepo_root()detection (looks foryarn.lock,pnpm-workspace.yaml,lerna.json,package-lock.json+node_modulesin parent directories) and injects the monorepo root'snode_modulesintomoduleDirectoriesin the runtime Jest config.Cannot find moduleerrors for workspace packages like@strapi/utils,@strapi/types, etc. due to this issue.Test plan
_find_monorepo_root()(finds yarn workspace, returns None for standalone)_create_runtime_jest_config()(includesmoduleDirectoriesfor monorepo, omits for standalone, spreads base config)uv run prekpasses🤖 Generated with Claude Code