change-case icon indicating copy to clipboard operation
change-case copied to clipboard

Cannot compile latest version with Jest

Open mmick66 opened this issue 11 months ago • 3 comments

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

screenshot

What is happening?

mmick66 avatar Jan 13 '25 08:01 mmick66

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.

cdalvaro avatar Feb 18 '25 11:02 cdalvaro

@mmick66, @cdalvaro I faced same problem here, but recently found a solution. Try this:

  1. Add these lines to your jest config:
{
  "preset": "ts-jest",
  "testEnvironment": "node",
  "transformIgnorePatterns": [
    "/node_modules/(?!change-case/)"
  ],
  "transform": {
    "\\.[j]sx?$": "babel-jest"
  }
}
  1. Install package @babel/preset-env as devDependency

  2. 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:

  1. Add these lines to your jest config:
{
  "testEnvironment": "node",
  "transformIgnorePatterns": [
    "/node_modules/(?!change-case/)"
  ],
  "transform": {
    "\\.[j]sx?$": "babel-jest"
    "\\.[t]sx?$": "ts-jest"
  }
}
  1. Install next packages as devDeps:
babel-jest @babel/core @babel/preset-env
  1. Create babel.config.js and add these lines:
module.exports = {
  "presets": [
    "@babel/preset-env"
  ]
};

Hope this helps!

LarendsD avatar Feb 20 '25 12:02 LarendsD

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
};

cdalvaro avatar Feb 21 '25 10:02 cdalvaro