NodeJS Typescript未选取Jest类型

gt0wga4j  于 2022-12-22  发布在  Node.js
关注(0)|答案(3)|浏览(122)

问题
我正在尝试用jest设置一个typescript项目。但是typescript似乎没有从@types/jest中拾取jest类型,@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"],
}
  • 下面是我的jest.config.js
module.exports = {
  preset: 'ts-jest',
  roots: ['<rootDir>/src'],
  testEnvironment: 'node',
  moduleFileExtensions: ['ts', 'js', 'json', 'node'],
};
  • 这是我的. babelrc
{
  "presets": [
    ["@babel/preset-env", {"targets": {"node": "current"}}],
    "@babel/preset-typescript"
  ],
  "plugins": ["@babel/transform-runtime"]
}
  • 下面是我的package.json中的开发依赖项

我的测试运行良好的npm run test,但它仍然是一个恼人的问题,我想这只是一个失踪的./在我的配置或东西之一。位新的 typescript /笑话虽然所以可能是其他东西。希望我已经提供了足够的信息,但我很乐意添加更多,如果必要的任何帮助将不胜感激,干杯

vatpfxk5

vatpfxk51#

如果有人遇到类似的问题。请检查tsconfig中的以下字段

"include": ["src/**/*"]

请确保您已包含该文件

"exclude": ["node_modules", "dist"]

最后检查您是否指定了typeRoots,默认情况下typeRoots指向./node_modules/@types,并且types包含jest

kqlmhetl

kqlmhetl2#

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

{
"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"]
}
ojsjcaue

ojsjcaue3#

有一种可能性,即依赖关系树已损坏。
杀了两个人。
然后运行npm i

相关问题