Cannot compile latest version with Jest
I am using Node v22.12.0 with NestJS and Jest frameworks (latest versions). When I include the project in Nest it runs fine, but when I try in on Jest I get the following error
What is happening?
Same thing here.
I've tried adding the following config to the jest.config.js file:
module.exports = {
preset: "ts-jest",
transform: {
"^.+\\.tsx?$": "ts-jest",
},
transformIgnorePatterns: ["/node_modules/(?!(title-case)/)"],
// Other settings
};
But it doesn't solve the issue.
@mmick66, @cdalvaro I faced same problem here, but recently found a solution. Try this:
- Add these lines to your jest config:
{
"preset": "ts-jest",
"testEnvironment": "node",
"transformIgnorePatterns": [
"/node_modules/(?!change-case/)"
],
"transform": {
"\\.[j]sx?$": "babel-jest"
}
}
-
Install package @babel/preset-env as devDependency
-
Create babel.config.js file in root and add these lines:
module.exports = {
"presets": [
"@babel/preset-env"
]
};
This is my simplified solution, if this not helps, try this:
- Add these lines to your jest config:
{
"testEnvironment": "node",
"transformIgnorePatterns": [
"/node_modules/(?!change-case/)"
],
"transform": {
"\\.[j]sx?$": "babel-jest"
"\\.[t]sx?$": "ts-jest"
}
}
- Install next packages as devDeps:
babel-jest @babel/core @babel/preset-env
- Create babel.config.js and add these lines:
module.exports = {
"presets": [
"@babel/preset-env"
]
};
Hope this helps!
Thank you for your reply @LarendsD!
In my case, I have updated jest and ts-jest to the current latest version, ^29.7.0 and ^29.2.5, respectively. And after that, I've installed: babel-jest, @babel/preset-typescript, @babel/core and @babel/preset-env.
With that packages, these are the config files that have worked for me:
// babel.config.js
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
// jest.config.js
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
transformIgnorePatterns: ["/node_modules/(?!title-case/)"],
transform: {
"\\.[j]sx?$": "babel-jest",
"\\.[t]sx?$": "ts-jest",
},
// Other config
};