Skip to content

Commit

Permalink
fix(cra): Fix jest-watch-typeahead resolution
Browse files Browse the repository at this point in the history
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
NiGhTTraX committed Dec 28, 2021
1 parent d60a6f5 commit d931391
Show file tree
Hide file tree
Showing 2 changed files with 1,742 additions and 3,835 deletions.
1 change: 1 addition & 0 deletions examples/cra/package.json
Expand Up @@ -16,6 +16,7 @@
"@craco/craco": "~6.4.0",
"cross-env": "~7.0.2",
"react-scripts": "~4.0.3",
"jest-watch-typeahead": "~0.6.4",
"fork-ts-checker-webpack-plugin": "~6.5.0",
"tsconfig-paths-webpack-plugin": "~3.5.0"
},
Expand Down

0 comments on commit d931391

Please sign in to comment.