Search code examples
jestjsyarnpkgyarn-v2

How to patch jest-rutime when using Yarn 2?


I am trying to follow the instructions in this repository to patch Jest.

Patch Jest.

It is suggested to use patch-package but I figured out that I can use yarn patch when using Yarn 2.

I managed to patch jest-runtime but seems Jest doesn’t seem to require jest-runtime in its package so I don’t know where it comes from to use it as a reference to declare the patched file.

Jest package.json

I understand if Jest was the one that needs to be patched I could declare it like this:

package.json

"devDependencies": {
   "jest": "patch:[email protected]#./patches/jest.patch"
}

I tried to use the same logic to include the following code to include jest-runtime but it didn't work.

"devDependencies": {
   "jest": "^26.6.3",
   "jest-runtime": "patch:[email protected]#./patches/jest-runtime.patch"
}

How can I declare this patched jest-runtime so Jest can use it?


Solution

  • The Resolutions field in the manifest is the correct approach to declare the patched modules that we didn't add to devDependencies such as submodules.

    The resolutions field allows you to instruct Yarn to use a specific resolution instead of anything the resolver would normally pick. This is useful to enforce all your packages to use a single version of a dependency, or backport a fix.

    The fix for that issue:

    {
     ...
      "dependencies": {
        "jest": "^26.6.3",
      },
      "resolutions": {
        "jest-runtime": "patch:[email protected]#./patches/jest-runtime.patch"
      },
    }