Jest&TypeScript:VS代码找不到名称

问题描述

我将Jest与TypeScript一起使用。尽管我的代码可以工作并且可以构建我的项目,但是Visual Studio Code会向我抛出所有Jest方法describe()test() ...)的错误

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

我有srctests个分开的目录。我遵循在Internet上找到的配置,但是它没有任何改变,我缺少什么?到目前为止,唯一的方法是将我的tests文件包括include的{​​{1}}设置中,这很不好,因为它是建立在tsconfig目录中的。

已安装Dev依赖项:dist

jest ts-jest @types/jest

tsconfig.json

{ "compilerOptions": { "sourceMap": true,"module": "commonjs","moduleResolution": "node","esModuleInterop": true,"allowJs": true,"jsx": "react","target": "es5","lib": ["dom","dom.iterable","esnext"],"baseUrl": "./","paths": { "*": ["src/*"] },"typeRoots": ["./node_modules/@types"],"types": ["node","jest"] },"strict": true,"compileOnSave": false,"include": ["src"] }

jest.config.js

解决方法

只需在您的jest中将typeAcquisition作为tsconfig.json包括在内,

// tsconfig.json
{
  "compilerOptions": { /* ... */ },"typeAcquisition": { "include": ["jest"] },// ... your other options go here
}