简体   繁体   中英

Jest Type definitions are not working on a Typescript project

I've started a new Typescript project with GTS . The typings are working fine for regular *.ts files but nothing I've tried works to get *.spec.ts files to work.

Problem

Every jest function is marked red by ESLint in VS code. The message will say "(describe | it | expect) is not defined". I have tried including and excluding files, adding types, and copying from other projects.

Here is my current tsconfig:

{
  "compileOnSave": false,
  "extends": "./node_modules/gts/tsconfig-google.json",
  "compilerOptions": {
    "rootDir": "./src",
    "outDir": "./dist",
    "sourceMap": true,
    "declaration": true,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "module": "esnext",
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2017", "dom"],
    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "baseUrl": ".",
    "types": ["node", "jest"]
  },
  "exclude": ["**/*.spec.ts"],
  "include": ["src/**/*.ts", "tests/**/*.ts"]
}

The root directory is simple

  • src (ts files)
  • tests (all spec files)
  • tsconfig.json

Relevant Packages:

"@types/jest": "^26.0.14",
"@types/node": "^13.11.1",
"gts": "^2.0.2",
"jest": "^26.4.2",
"tslint": "^6.1.3",
"typescript": "^4.0.3"

Is there something obvious I have done wrong in my config? Should I have 2 configs, one for ts, the other for spec?

The problem was ESLint configuration . I added this to the .eslintrc.json file and the errors went away:

"env": {
    "browser": true,
    "es6": true,
    "jest": true,
    "node": true
  },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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