最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript is not picking up Jest types - Stack Overflow

programmeradmin1浏览0评论

The Problem

I am trying to setup a typescript project with jest. However typescript does not seem to pickup the jest types from @types/jest, which is highlighting my keywords giving me the following error:

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)

Notes:

  • I am using VS Code
  • I have installed @types/jest
  • I have attempted to reinstall all my packages
  • I have reloaded my editor
  • Here is my tsconfig.json
{
  "compilerOptions": {
    "types": ["jest", "node"]
  },
  "types": ["jest"],
  "typeRoots": ["./src/types", "./node_modules/@types"],
}
  • Here is my jest.config.js
module.exports = {
  preset: 'ts-jest',
  roots: ['<rootDir>/src'],
  testEnvironment: 'node',
  moduleFileExtensions: ['ts', 'js', 'json', 'node'],
};
  • Here is my .babelrc
{
  "presets": [
    ["@babel/preset-env", {"targets": {"node": "current"}}],
    "@babel/preset-typescript"
  ],
  "plugins": ["@babel/transform-runtime"]
}
  • Here are the dev dependencies in my package.json

My test runs fine with npm run test, But it is still an annoying issue which I imagine is just a missing ./ in one of my configurations or something. Bit new to typescript/jest though so could be something else. Hopefully I have provided enough info, but am happy to add more if necessary Any help would be appreciated, cheers

The Problem

I am trying to setup a typescript project with jest. However typescript does not seem to pickup the jest types from @types/jest, which is highlighting my keywords giving me the following error:

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)

Notes:

  • I am using VS Code
  • I have installed @types/jest
  • I have attempted to reinstall all my packages
  • I have reloaded my editor
  • Here is my tsconfig.json
{
  "compilerOptions": {
    "types": ["jest", "node"]
  },
  "types": ["jest"],
  "typeRoots": ["./src/types", "./node_modules/@types"],
}
  • Here is my jest.config.js
module.exports = {
  preset: 'ts-jest',
  roots: ['<rootDir>/src'],
  testEnvironment: 'node',
  moduleFileExtensions: ['ts', 'js', 'json', 'node'],
};
  • Here is my .babelrc
{
  "presets": [
    ["@babel/preset-env", {"targets": {"node": "current"}}],
    "@babel/preset-typescript"
  ],
  "plugins": ["@babel/transform-runtime"]
}
  • Here are the dev dependencies in my package.json

My test runs fine with npm run test, But it is still an annoying issue which I imagine is just a missing ./ in one of my configurations or something. Bit new to typescript/jest though so could be something else. Hopefully I have provided enough info, but am happy to add more if necessary Any help would be appreciated, cheers

Share Improve this question asked Dec 2, 2020 at 10:02 ProvingBardProvingBard 3831 gold badge5 silver badges16 bronze badges 3
  • stackoverflow.com/questions/42024526/… – LogiStack Commented Dec 2, 2020 at 10:06
  • @StefanWang I have installed @types/jest already and have jest as a dependency in my package json. I attempted the typeAquisition solution, but no luck there either
发布评论

评论列表(0)

  1. 暂无评论