(警告)在“globals”下定义“ts-jest”配置已被弃用

Rom*_*man 27 unit-testing jestjs ts-jest

我有"jest": "^29.2.1"。我的单元测试输出错误如下:

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[backports] (WARN) "[jest-config].globals.ts-jest.tsConfig" is deprecated, use "[jest-config].globals.ts-jest.tsconfig" instead.
ts-jest[backports] (WARN) Your Jest configuration is outdated. Use the CLI to help migrating it: ts-jest config:migrate <config-file>.
Run Code Online (Sandbox Code Playgroud)

jest.config.ts的如下:

const config = {
  roots: ['<rootDir>/src'],
  verbose: true,
  globalSetup: './globalSetup.ts',
  testEnvironmentOptions: {
    url: 'http://localhost/',
  },
  setupFilesAfterEnv: [],
  moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'],
  preset: 'ts-jest',
  transform: {
    '^.+\\.tsx?$': ['ts-jest', {}],
    '^.+\\.ts?$': ['ts-jest', {}],
  },
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
  },
  testMatch: [
    '<rootDir>/**/(*.)test.(js|jsx|ts|tsx)',
    '<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
    '<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}',
  ],
  globals: {
    'ts-jest': {
      babel: true,
      tsConfig: 'tsconfig.json',
    },
  },
}

module.exports = config
Run Code Online (Sandbox Code Playgroud)

小智 38

我有过类似的警告,解决方案已经在答案中(但我第一次不明白)你必须将内容移到按键globalstransform。该文件必须如下所示:

const config = {
  roots: ['<rootDir>/src'],
  verbose: true,
  globalSetup: './globalSetup.ts',
  testEnvironmentOptions: {
    url: 'http://localhost/',
  },
  setupFilesAfterEnv: [],
  moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'],
  preset: 'ts-jest',
  transform: {
    '^.+\\.tsx?$': ['ts-jest', {//the content you'd placed at "global"
      babel: true,
      tsConfig: 'tsconfig.json',
    }]
  },
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
  },
  testMatch: [
    '<rootDir>/**/(*.)test.(js|jsx|ts|tsx)',
    '<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
    '<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}',
  ]
}

module.exports = config
Run Code Online (Sandbox Code Playgroud)

另外,似乎您可以跳过第二个转换(正则表达式表达式相同),第一个转换适用于.ts.tsx文件,第二个转换适用于.t.ts

  • 我已经这样做了,但 Jest 仍然给我同样的警告“Define \`ts-jest\` config under \`globals\` is deprecated.”。当我在代码库中搜索“globals”时,有 0 个结果。为什么它仍然警告我这个?有什么指点吗? (4认同)

Rom*_*man -1

我通过删除整个财产来解决这个问题globals。结果目前的状态const config.ts如下:

const config = {
  roots: ['<rootDir>/src'],
  verbose: true,
  globalSetup: './globalSetup.ts',
  testEnvironmentOptions: {
    url: 'http://localhost/',
  },
  setupFilesAfterEnv: [],
  moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'],
  preset: 'ts-jest',
  transform: {
    '^.+\\.{ts|tsx}?$': ['ts-jest', {
      babel: true,
      tsConfig: 'tsconfig.json',
    }],

  },
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
  },
  testMatch: [
    '<rootDir>/**/(*.)test.(js|jsx|ts|tsx)',
    '<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
    '<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}',
  ],
}

module.exports = config

Run Code Online (Sandbox Code Playgroud)