问题描述
我将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)
我有src
和tests
个分开的目录。我遵循在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
}