Typescript 没有选择 Jest 类型

Pro*_*ard 9 javascript config node.js typescript jestjs

问题

我正在尝试用笑话设置一个打字稿项目。然而,打字稿似乎没有从 中获取笑话类型@types/jest,这突出显示了我的关键字,给了我以下错误:

Cannot find name 'test'. Do you need to install type definitions for a test runner? Try 'npm i @types/jest' or 'npm i @types/mocha'. ts(2582)

在此输入图像描述

笔记:

  • 我正在使用 VS 代码
  • 我已经安装了@types/jest
  • 我尝试重新安装所有软件包
  • 我已经重新加载了我的编辑器
  • 这是我的 tsconfig.json
{
  "compilerOptions": {
    "types": ["jest", "node"]
  },
  "types": ["jest"],
  "typeRoots": ["./src/types", "./node_modules/@types"],
}
Run Code Online (Sandbox Code Playgroud)
  • 这是我的 jest.config.js
module.exports = {
  preset: 'ts-jest',
  roots: ['<rootDir>/src'],
  testEnvironment: 'node',
  moduleFileExtensions: ['ts', 'js', 'json', 'node'],
};
Run Code Online (Sandbox Code Playgroud)
  • 这是我的 .babelrc
{
  "presets": [
    ["@babel/preset-env", {"targets": {"node": "current"}}],
    "@babel/preset-typescript"
  ],
  "plugins": ["@babel/transform-runtime"]
}
Run Code Online (Sandbox Code Playgroud)
  • 这是我的 package.json 中的开发依赖项

在此输入图像描述

我的测试运行良好npm run test,但这仍然是一个恼人的问题,我想这只是./我的配置之一或其他内容中的缺失。虽然打字稿/笑话有点新鲜,但可能是别的东西。希望我提供了足够的信息,但如果有必要,我很乐意添加更多信息 任何帮助将不胜感激,干杯

小智 4

我不是专家,但在修改 typeRoots 属性后,我遇到了同样的问题(尽管设置不同),并且我能够通过在“types”属性中包含“@types/jest”来使其工作tsconfig.json

{
"compilerOptions": {
    "target": "es6",                          
    "module": "commonjs",                    
    "outDir": "./dist",                        
    "rootDir": "./",                      
    "strict": true,                           
    "typeRoots": [      
    "./node_modules/@types/",
    "./src/@types/"
    ],                       
    "types": ["jest", "node", "@types/jest"],                          
    "esModuleInterop": true,                  
    "skipLibCheck": true,                    
    "forceConsistentCasingInFileNames": true  
},
"exclude": ["node_modules"],
"include": ["./src/**/*.tsx", "./src/**/*.ts"]
}
Run Code Online (Sandbox Code Playgroud)