【问题标题】:Typescript is not picking up Jest typesTypescript 没有选择 Jest 类型
【发布时间】:2021-03-14 06:46:21
【问题描述】:

问题

我正在尝试用 jest 设置一个打字稿项目。但是 typescript 似乎没有从@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/jest 有点陌生,但可能是别的东西。希望我已经提供了足够的信息,但如果有必要,我很乐意添加更多任何帮助将不胜感激,干杯

【问题讨论】:

  • @StefanWang 我已经安装了@types/jest,并且在我的包 json 中有 jest 作为依赖项。我尝试了 typeAquisition 解决方案,但也没有运气????谢谢你的建议

标签: javascript node.js typescript jestjs config


【解决方案1】:

依赖关系树有可能损坏。
杀死node_modules & package-lock.json.
然后运行npm i

【讨论】:

    【解决方案2】:

    我不是专家,但我在修改 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"]
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-31
      • 2021-01-09
      • 2018-12-18
      • 2023-01-10
      • 2019-09-08
      • 1970-01-01
      • 2023-01-17
      • 2021-01-19
      • 2019-10-01
      相关资源
      最近更新 更多