简体   繁体   English

为什么 ts-jest 不加载我的自定义 tsconfig 文件?

[英]Why isn't ts-jest loading my custom tsconfig file?

For some reason, my custom tsconfig file isn't being picked up in jest.config.ts.出于某种原因,我的自定义 tsconfig 文件没有在 jest.config.ts 中被拾取。 Here is my jest.config.ts:这是我的 jest.config.ts:

module.exports = {
  setupFilesAfterEnv: [`./jest.setup.ts`],
  testEnvironment: `jsdom`,
  roots: [
    `<rootDir>/test`
  ],
  testMatch: [
    `**/__tests__/**/*.+(ts|tsx|js)`,
    `**/?(*.)+(spec|test).+(ts|tsx|js)`
  ],
  transform: {
    "^.+\\.(ts|tsx)$": `ts-jest`
  },
  globals: {
    "ts-jest": {
      tsConfig: `tsconfig.jest.json`
    }
  }
}

I know that other parts of this config ARE being applied.我知道正在应用此配置的其他部分。 For example, if I remove the keys above globals my tests don't run.例如,如果我删除全局变量上方的键,我的测试将不会运行。 Also, I know that the change specified in my tsconfig.jest.json file is the necessary change to fix my tests because if I make the same change in my main tsconfig.json file my tests run fine.另外,我知道在我的 tsconfig.jest.json 文件中指定的更改是修复我的测试的必要更改,因为如果我在我的主 tsconfig.json 文件中进行相同的更改,我的测试运行良好。

I've also tried putting to desired tsconfig compiler options directly into the jest.config.ts file, but that doesn't seem to work either:我还尝试将所需的 tsconfig 编译器选项直接放入 jest.config.ts 文件中,但这似乎也不起作用:

module.exports = {
  setupFilesAfterEnv: [`./jest.setup.ts`],
  testEnvironment: `jsdom`,
  roots: [
    `<rootDir>/test`
  ],
  testMatch: [
    `**/__tests__/**/*.+(ts|tsx|js)`,
    `**/?(*.)+(spec|test).+(ts|tsx|js)`
  ],
  transform: {
    "^.+\\.(ts|tsx)$": `ts-jest`
  },
  globals: {
    "ts-jest": {
      tsConfig: {
        jsx: `react`
      }
    }
  }
}

the correct key is tsconfig not tsConfig .正确的键是tsconfig而不是tsConfig

Updated answer for jest@29 (released August 2022) and ts-jest@29 (released September 2022) jest@29(2022 年 8 月发布)和 ts-jest@29(2022 年 9 月发布)的更新答案

All of my React component *.tsx -related tests were broken after upgrading Jest-and-friends to version 29. I also received error messages that jestConfig.globals['ts-jest'] is now deprecated.在将 Jest-and-friends 升级到版本 29 后,我所有与 React 组件*.tsx相关的测试都被破坏了。我还收到错误消息,指出jestConfig.globals['ts-jest']现在已弃用。

As per the ts-jest "TypeScript Config option" docs you should modify your Jest config to jestConfig.transform['regex_match_files'] .根据ts-jest "TypeScript Config option" 文档,您应该将 Jest 配置修改为jestConfig.transform['regex_match_files'] The config for my project is below.我的项目的配置如下。 (Caveat: note that I am using a custom location for my config files, ie a dedicated /config/ directory, but the principle of specifying the relative-path-to-config-file remains the same). (注意:请注意,我正在为我的配置文件使用自定义位置,即专用/config/目录,但指定相对路径到配置文件的原则保持不变)。

  transform: {
    '^.+\\.tsx?$': [
      'ts-jest',
      // required due to custom location of tsconfig.json configuration file
      // https://kulshekhar.github.io/ts-jest/docs/getting-started/options/tsconfig/
      {tsconfig: './config/tsconfig.json'},
    ],
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM