Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts-node not found with TS config file and yarn 2 PNP strict mode + workspaces #11751

Closed
blutorange opened this issue Aug 14, 2021 · 6 comments
Closed

Comments

@blutorange
Copy link

🐛 Bug Report

Jest supports writing configuration files in TypeScript, but it does not work when using Yarn 2 PNP strict mode combined with workspaces.

As far as I could find out: Yarn PNP strict mode requires that all packages declare the modules they are using. ts-config declares ts-node as an optional peer dependency, but it seems some other jest packages do not. As a workaround, I can patch the packages via the following entries is .yarnrc.yml

packageExtensions:
  "jest@*":
    peerDependencies:
      "ts-node": "*"
    peerDependenciesMeta:
      "ts-node":
        optional: true

  "jest-cli@*":
    peerDependencies:
      ts-node: "*"
    peerDependenciesMeta:
      "@types/node":
        optional: true

  "@jest/core@*":
    peerDependencies:
      ts-node: "*"
    peerDependenciesMeta:
      "ts-node":
        optional: true

To Reproduce

Seems to happen when (see git repo below):

  • yarn 2 package
  • PNP and strict mode enabled
  • a root workspace and a child package called common
  • root package just has a package.json with a workspace entry
  • inside the child package
    • a jest.config.ts config file written in TypeScript
    • a __test__/util.spec.ts test written in TypeScript
    • jest, ts-jest, ts-node, tslib, typescript are installed as devDependencies.

Running jest (yarn workspace common run test) now results in the following error:

Error: Jest: Failed to parse the TypeScript config file /home/user/git/my-project/src/main/web/packages/common/jest.config.ts
  Error: Jest: 'ts-node' is required for the TypeScript configuration files. Make sure it is installed
Error: jest-config tried to access ts-node (a peer dependency) but it isn't provided by its ancestors; this makes the require call ambiguous and unsound.

Required package: ts-node
Required by: jest-config@virtual:8a2bfd0b48fab47c142a672c61344badd804b2b98eaa4dbdf39b003802f04821a4d722c59106de2a566f324d0b00108353fa24814d849ad15543e070150d5a79#npm:27.0.6::__archiveUrl=https%3A%2F%2Fsome.artifactory.de%3A443%2Fartifactory%2Fapi%2Fnpm%2Fnpm-repo%2Fjest-config%2F-%2Fjest-config-27.0.6.tgz (via /home/user/git/my-project/src/main/web/.yarn/__virtual__/jest-config-virtual-b459589e8b/6/.yarn/berry/cache/jest-config-npm-27.0.6-08f45f5abd-8.zip/node_modules/jest-config/build/)

Ancestor breaking the chain: @jest/core@virtual:39d42eb86538fec3152dc6649557bb176a01eddf97f7e4d3484f986a3275c84421224da52a6ffdbd6b400f55b6edfeef7b6c99c49d1058e6dd5b05e3abf5895e#npm:27.0.6::__archiveUrl=https%3A%2F%2Fsome.artifactory.de%3A443%2Fartifactory%2Fapi%2Fnpm%2Fnpm-repo%2F%40jest%2Fcore%2F-%2Fcore-27.0.6.tgz
Ancestor breaking the chain: jest-cli@virtual:39d42eb86538fec3152dc6649557bb176a01eddf97f7e4d3484f986a3275c84421224da52a6ffdbd6b400f55b6edfeef7b6c99c49d1058e6dd5b05e3abf5895e#npm:27.0.6::__archiveUrl=https%3A%2F%2Fsome.artifactory.de%3A443%2Fartifactory%2Fapi%2Fnpm%2Fnpm-repo%2Fjest-cli%2F-%2Fjest-cli-27.0.6.tgz

Require stack:
- /home/user/git/my-project/src/main/web/.yarn/__virtual__/jest-config-virtual-b459589e8b/6/.yarn/berry/cache/jest-config-npm-27.0.6-08f45f5abd-8.zip/node_modules/jest-config/build/readConfigFileAndSetRootDir.js
- /home/user/git/my-project/src/main/web/.yarn/__virtual__/jest-config-virtual-b459589e8b/6/.yarn/berry/cache/jest-config-npm-27.0.6-08f45f5abd-8.zip/node_modules/jest-config/build/index.js
- /home/user/git/my-project/src/main/web/.yarn/__virtual__/jest-cli-virtual-d0eb696a0d/6/.yarn/berry/cache/jest-cli-npm-27.0.6-b581fb3fb0-8.zip/node_modules/jest-cli/build/cli/index.js
- /home/user/git/my-project/src/main/web/.yarn/__virtual__/jest-cli-virtual-d0eb696a0d/6/.yarn/berry/cache/jest-cli-npm-27.0.6-b581fb3fb0-8.zip/node_modules/jest-cli/bin/jest.js
- /home/user/git/my-project/src/main/web/.yarn/__virtual__/jest-virtual-39d42eb865/6/.yarn/berry/cache/jest-npm-27.0.6-5fba1e806d-8.zip/node_modules/jest/bin/jest.js
    at readConfigFileAndSetRootDir (/home/user/git/my-project/src/main/web/.yarn/__virtual__/jest-config-virtual-b459589e8b/6/.yarn/berry/cache/jest-config-npm-27.0.6-08f45f5abd-8.zip/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:118:13)
    at async readConfig (/home/user/git/my-project/src/main/web/.yarn/__virtual__/jest-config-virtual-b459589e8b/6/.yarn/berry/cache/jest-config-npm-27.0.6-08f45f5abd-8.zip/node_modules/jest-config/build/index.js:227:18)
    at async readConfigs (/home/user/git/my-project/src/main/web/.yarn/__virtual__/jest-config-virtual-b459589e8b/6/.yarn/berry/cache/jest-config-npm-27.0.6-08f45f5abd-8.zip/node_modules/jest-config/build/index.js:412:26)
    at async runCLI (/home/user/git/my-project/src/main/web/.yarn/__virtual__/@jest-core-virtual-8a2bfd0b48/6/.yarn/berry/cache/@jest-core-npm-27.0.6-817fd8fcee-8.zip/node_modules/@jest/core/build/cli/index.js:220:59)
    at async Object.run (/home/user/git/my-project/src/main/web/.yarn/__virtual__/jest-cli-virtual-d0eb696a0d/6/.yarn/berry/cache/jest-cli-npm-27.0.6-b581fb3fb0-8.zip/node_modules/jest-cli/build/cli/index.js:163:37)

Expected behavior

Jest runs successfully, yarn can load ts-node.

Link to repl or repo (highly encouraged)

https://github.com/blutorange/bug-jest-ts-config-with-yarn-pnp-strict

# if you haven't installed yarn yet
npm install -g yarn

git clone https://github.com/blutorange/bug-jest-ts-config-with-yarn-pnp-strict
cd bug-jest-ts-config-with-yarn-pnp-strict
yarn install
yarn workspace common run test

If you now open .yarnrc.yml, uncomment the packageExtensions section, and run yarn install followed by yarn workspace common run test again, it should run the test successfully.

@sigveio
Copy link
Contributor

sigveio commented Aug 17, 2021

Thanks for posting thorough reproduction steps, @blutorange. =)

I'm admittedly not deeply familiar with PnP, and as such don't have much to add; but judging from the errors and require stack trace it does seem like the preceding packages in the chain might also have to provide ts-node as an optional peerDep to be compatible with its strict mode. Like you have done with your packageExtensions workaround.

I would defer to @SimenB's wisdom on this (and to what extent Jest wants to support strict package managers)

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 17, 2023
@blutorange
Copy link
Author

I updated the example repo with the latest versions of yarn and jest. The issue still occurs. Since yarn is getting more usage, it would be great if this could be resolved.

Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 17, 2024
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 18, 2024
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants