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

[Bug]: Declare ts-node as dependency of jest-config #14752

Closed
yohny opened this issue Dec 12, 2023 · 16 comments
Closed

[Bug]: Declare ts-node as dependency of jest-config #14752

yohny opened this issue Dec 12, 2023 · 16 comments

Comments

@yohny
Copy link

yohny commented Dec 12, 2023

Version

29.7.0

Steps to reproduce

When you have a new project with Jest set up as testing framework and you are using a typescript configuration files you get following error when launching jest tests:

Error: Jest: Failed to parse the TypeScript config file C:\Users\nej\source\repos\ddnp-gui\jest.config.ts
  Error: Jest: 'ts-node' is required for the TypeScript configuration files. Make sure it is installed
Error: Cannot find package 'ts-node' imported from C:\Users\nej\source\repos\ddnp-gui\node_modules\jest-config\build\readConfigFileAndSetRootDir.js
    at readConfigFileAndSetRootDir (C:\Users\nej\source\repos\ddnp-gui\node_modules\jest-config\build\readConfigFileAndSetRootDir.js:116:13)
    at async readInitialOptions (C:\Users\nej\source\repos\ddnp-gui\node_modules\jest-config\build\index.js:403:13)
    at async readConfig (C:\Users\nej\source\repos\ddnp-gui\node_modules\jest-config\build\index.js:147:48)
    at async readConfigs (C:\Users\nej\source\repos\ddnp-gui\node_modules\jest-config\build\index.js:424:26)

Expected behavior

Threre should be no error as typescript configuration is supported according to docs. So expected behaviour is that ts-node should be declared as peerDependency of jest-config. Once ts-node is installed alll works as expected.

Actual behavior

You get error above when running jest tests using typescript configuration, because ts-node is not declared as dependency of jest-config (its only in devDependencies of jest-config).

Additional context

No response

Environment

System:
  OS: Windows 10 10.0.19045
  CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
Binaries:
  Node: 20.10.0 - C:\Program Files\nodejs\node.EXE
  npm: 10.2.3 - C:\Program Files\nodejs\npm.CMD
npmPackages:
  jest: ^29.7.0 => 29.7.0
@habtim
Copy link

habtim commented Dec 13, 2023

Hey @yohny
As stated https://jestjs.io/docs/getting-started#using-typescript, you can either use babel or ts-node.
None of which is installed by default and it's up to you to define which way to go.
Also note (when using ts-node)

In order for Jest to transpile TypeScript with ts-jest, you may also need to create a configuration file.

Should work as expected.

@yohny
Copy link
Author

yohny commented Dec 16, 2023

yes, the docs mention 2 alternatives - babel and ts-jest (not ts-node), but for babel case it is only mentioned to install @babel/preset-typescript and for ts-jest case its only mentioned to install ts-jest. Nothing about the need to install ts-node. Can than at least the docs be updated to reflect the reality?
Also when ts-node is required to be installed for ts-jest alternative to work, should it not be in its peerDependencies? Isn't that what peerDependencies are for? Or am I missing something?

Copy link

This issue is stale because it has been open 30 days 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 Jan 15, 2024
@yohny
Copy link
Author

yohny commented Jan 15, 2024

ping, to prevent stale bot from closing

@github-actions github-actions bot removed the Stale label Jan 15, 2024
Copy link

This issue is stale because it has been open 30 days 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 Mar 13, 2024
@yohny
Copy link
Author

yohny commented Mar 13, 2024

ping, to prevent stale bot from closing

@github-actions github-actions bot removed the Stale label Mar 13, 2024
@djsiddz
Copy link

djsiddz commented Apr 4, 2024

If someone needs a temporary workaround using yarn, I'm leaving this code here. Add a .yarnrc.yml file to the root of your project. Include the below code:

packageExtensions: {
  jest-config@29.7.0: {
    dependencies: {
      ts-node: ">=9.0.0"
    }
  }
}

After adding the file, run yarn install. Then yarn build or yarn dev should work fine. It worked for me.

I faced this issue with Jest v29.7.0. I've assumed jest-config's version to be in sync with jest.
Also ts-node's version has been picked from jest-config's package.json here available today.

Once this is fixed in Jest's future release, the .yarnrc.yml file or at least the above lines can be safely removed from this file.

P.S.: Possibly, your first thought would be to use yarn patch. However, the dependencies are computed at fetch time and not at resolution time. Hence packageExtensions is the way to go.

References:
Yarn Package Patching - See Limitations
Yarn Settings (.yarnrc.yml) - See packageExtensions

Learnt this when I faced a similar issue with Astro here.

Hope this helps!

@marco-carvalho
Copy link

@SimenB shouldn't ts-node be in dependencies of jest-config? https://github.com/jestjs/jest/blob/main/packages/jest-config/package.json

Copy link

github-actions bot commented Jun 2, 2024

This issue is stale because it has been open 30 days 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 Jun 2, 2024
@marco-carvalho
Copy link

ping to prevent stale bot from closing

@github-actions github-actions bot removed the Stale label Jun 3, 2024
Copy link

github-actions bot commented Jul 3, 2024

This issue is stale because it has been open 30 days 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 Jul 3, 2024
@yohny
Copy link
Author

yohny commented Jul 4, 2024

ping, to prevent stale bot from closing

@github-actions github-actions bot removed the Stale label Jul 4, 2024
Copy link

github-actions bot commented Aug 3, 2024

This issue is stale because it has been open 30 days 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 Aug 3, 2024
@yohny
Copy link
Author

yohny commented Aug 4, 2024

ping, to prevent stale bot from closing

@github-actions github-actions bot removed the Stale label Aug 4, 2024
@SimenB
Copy link
Member

SimenB commented Aug 5, 2024

We have recently added support for esbuild-register in addition to ts-node, so it makes sense for the consumer to bring the TS implementation they would like.

We're also considering not using ts-node as default (so you would have to explicitly opt-in to it.)

@SimenB SimenB closed this as completed Aug 5, 2024
Copy link

github-actions bot commented Sep 6, 2024

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 Sep 6, 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

5 participants