Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cra): Fix
jest-watch-typeahead
resolution
Fixes #159. It seems like package managers can break the `watchPlugins` resolution in jest config by deciding to hoist or not to hoist some of react-script's dependencies (I believe `jest-config` in combination with `jest-watch-typeahead` is the most likely combo to cause the issue). `jest-config` (which does the `require.resolve`) gets hoisted to the top: ```console ❯ yarn why jest-config => Found "jest-config@26.6.3" info Has been hoisted to "jest-config" info Reasons this module exists - "workspace-aggregator-2a09db49-5090-4a4d-b8f3-5689124d9bcf" depends on it - Hoisted from "_project_#@jest#core#jest-config" - Hoisted from "_project_#jest-runner#jest-config" - Hoisted from "_project_#jest-runtime#jest-config" - Hoisted from "_project_#@NiGhTTraX#cra-example#react-scripts#jest#jest-cli#jest-config" ``` The watch plugin gets nested under `react-scripts`: ```console ❯ yarn why jest-watch-typeahead => Found "jest-watch-typeahead@0.6.1" info Reasons this module exists - "_project_#@NiGhTTraX#cra-example#react-scripts" depends on it - Hoisted from "_project_#@NiGhTTraX#cra-example#react-scripts#jest-watch-typeahead" ``` When the config is validated the plugin won't be found since it's not on the same level as `jest-config`. Different dependency graphs, and even different `yarn` runs can produce different trees, so this is one tricky issue to debug. Adding `jest-watch-typeahead` as a direct dependency in the CRA package _CAN_ cause it to be hoisted to the same level as `jest-config` (adding it in the monorepo root is probably safer): ```console ❯ yarn why jest-watch-typeahead => Found "jest-watch-typeahead@0.6.5" info Reasons this module exists - "_project_#@NiGhTTraX#cra-example" depends on it - Hoisted from "_project_#@NiGhTTraX#cra-example#jest-watch-typeahead" ``` It looks like `jest@27.0.4` [attempts to fix this](https://github.com/facebook/jest/pull/11493/files), though I'm not convinced it actually addresses the root issue, or just produces a different dependency tree by virtue of upgrading versions. ---- TL;DR: adding `jest-watch-typehead` in the CRA package or in the monorepo root _CAN_ solve the issue, though it depends on what other dependencies are present in the monorepo and where the package manager decides to place them. ---- Rant: `require()`-ing dependencies by strings from configs will never work the same for everyone. Hopefully tools (especially `jest`) will migrate to requiring the consumer to import them directly (this is impossible right now because jest configs are serialized to JSON).
- Loading branch information