Code Review Videos > How I Fixed > How I Fixed: (WARN) Define ts-jest config under globals is deprecated.

How I Fixed: (WARN) Define ts-jest config under globals is deprecated.

You know the drill: leave the office for the day on Wednesday, everything works. Open the laptop Thursday morning, errors everywhere. No? It can’t just be me… surely.

Well, today’s issue was when I ran my Jest tests, all of a sudden I started to see this:

ts-jest[ts-jest-transformer] (WARN) Define `ts-jest` config under `globals` is deprecated. Please do
transform: {
    <transform_regex>: ['ts-jest', { /* ts-jest config goes here in Jest */ }],
},
ts-jest[ts-jest-transformer] (WARN) Define `ts-jest` config under `globals` is deprecated. Please do
transform: {
    <transform_regex>: ['ts-jest', { /* ts-jest config goes here in Jest */ }],
},
ts-jest[ts-jest-transformer] (WARN) Define `ts-jest` config under `globals` is deprecated. Please do
transform: {
    <transform_regex>: ['ts-jest', { /* ts-jest config goes here in Jest */ }],
},
 PASS  src/extract-text-listing.test.ts
 FAIL  src/extract-all.test.ts
Code language: Shell Session (shell)

And this kinda tells you what is wrong.

Define `ts-jest` config under `globals` is deprecated

What is wrong

And then:

Please do transform: { <transform_regex>: [‘ts-jest’, { /* ts-jest config goes here in Jest */ }], },

How to fix it

Should be easy, right?

OK.

First thing, the package.json:

  "devDependencies": {
    "jest": "^29.7.0",
    "ts-jest": "^29.1.1"
  },Code language: JSON / JSON with Comments (json)

I’ve removed everything from there that isn’t relevant. I only include this to show you what versions I’m using.

Next, open up jest.config.ts:

export default {
  preset: "ts-jest",
  testEnvironment: "node",
  testMatch: ["<rootDir>/**/*.(spec|test).ts"],
  testPathIgnorePatterns: ["/node_modules/", "/dist/"],
  coverageDirectory: "./coverage",
  collectCoverageFrom: ["./src/**/*.ts"],
  coveragePathIgnorePatterns: ["node_modules"],
  coverageProvider: "v8",
  coverageReporters: ["json", "text", "lcov", "clover"],
  reporters: ["default"],
  globals: { "ts-jest": { diagnostics: false } },
  transform: {},
};
Code language: TypeScript (typescript)

I’ve highlighted the immediately problematic lines.

What the warning is telling us is that we need to move the config from globals to transform.

Only the message throws a curve ball in there:

transform: {
    <transform_regex>: ['ts-jest', { /* ts-jest config goes here in Jest */ }],
},Code language: JavaScript (javascript)

Somehow, someway, we need the transform_regex.

Fortunately, the regex, along with the change we need can be found on the ts-jest docs:

Therefore our change is to go from:

  globals: { "ts-jest": { diagnostics: false } },
  transform: {},Code language: TypeScript (typescript)

to:

  transform: {
    "^.+\\.tsx?$": ["ts-jest", { diagnostics: false }],
  },Code language: TypeScript (typescript)

Once you make that change, stop and restart Jest (e.g. if you’re running in Watch mode, it won’t take effect until you re-run Jest).

The regex works with .ts and .tsx files.

That’s everything I needed to do.

Strangely the screenshot above / docs say:

If you are using custom transform config, please remove preset from your Jest config to avoid issues that Jest doesn’t transform files correctly.

ts-jest docs caution

And I do have that, right at the top of my config:

export default {
  preset: "ts-jest",
Code language: TypeScript (typescript)

But if I remove that, I get a new error specific to my setup.

So for me, I’m keeping the preset and the custom regex… because that looks the most right. Or hides the errors from me, so I’m none the wiser.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.