Open yohny opened 7 months ago
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.
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?
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.
ping, to prevent stale bot from closing
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.
ping, to prevent stale bot from closing
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!
@SimenB shouldn't ts-node
be in dependencies
of jest-config
? https://github.com/jestjs/jest/blob/main/packages/jest-config/package.json
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.
ping to prevent stale bot from closing
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.
ping, to prevent stale bot from closing
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:
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. Oncets-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