question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Yarn 2 PnP and peer dependencies in Jest tests in workspace

See original GitHub issue
  • I’d be willing to implement a fix

Describe the bug

Peer dependencies of packages cannot be found when running Jest tests in workspace.

To Reproduce

I have created following minimal setup: https://github.com/kirill-konshin/yarn-berry-bug

Steps to reproduce:

$ git clone https://github.com/kirill-konshin/yarn-berry-bug.git
$ yarn
$ cd ws
$ yarn test

Output:

xxx@xxx ws % yarn test
 FAIL  src/index.test.js
  ● Test suite failed to run

    Cannot find module 'react' from 'index.js'

    However, Jest was able to find:
        './Router.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

      at Resolver.resolveModule (../.yarn/cache/jest-resolve-npm-25.1.0-2276a075e4-2.zip/node_modules/jest-resolve/build/index.js:276:11)
      at Object.<anonymous> (../.yarn/cache/create-react-class-npm-15.6.3-ae38dfa180-2.zip/node_modules/create-react-class/index.js:11:13)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.293s
Ran all test suites.

It looks like in the following setup require of react-router causes require of react in that module which can’t be found. Works perfect without PNP. Adding jest-pnp-resolver does not help.

Environment if relevant (please complete the following information):

  • OS: Mac
  • Node version: 12.11.1
  • Yarn version: 2.0.0-rc.29

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
arcaniscommented, Feb 15, 2020

Jest is “lying” and hiding the error that Yarn throws. It becomes very visible if you run your application with PNP_DEBUG_LEVEL=1 in your environment:

image

As you can see, the true error is:

error: Error: A package is trying to access another package without the second one being listed as a dependency of the first one

Required package: react (via "react")
Required by: create-react-class@npm:15.6.3 (via /home/arcanis/yarn-berry-bug/.yarn/cache/create-react-class-npm-15.6.3-ae38dfa180-2.zip/node_modules/create-react-class/)

By looking into the sources for create-react-class, we can see that they forgot to list the react peer dependency, hence why the resolution fails. Adding a packageExtensions entry fixes the issue:

packageExtensions:
    "create-react-class@*":
        peerDependencies:
            react: "*"

image

0reactions
kirill-konshincommented, Feb 18, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Migration | Yarn - Package Manager
A step-by-step and in-depth migration guide from Yarn 1 (Classic) to Yarn 2 (Berry).
Read more >
Newest 'yarn-v2' Questions
My project includes dependency X which in turn has subdependency Y. When I use yarn to start my project I get the error...
Read more >
yarn install peer dependencies automatically
A bug. What is the current behavior? After updating jest from 21.2.1 to 22.0.2 yarn install has some problems and causes TeamCity build...
Read more >
ts-jest
A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript. Latest version: 29.0.3, ...
Read more >
pnpm/pnpm - Gitter
readPackage: jest-runner: add jest-environment-jsdom as a peer dependency ... If you are simply trying to symlink two project folders together for testing, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found