Alp*_*a60 7 typescript tsconfig jestjs visual-studio-code ts-jest
我正在将 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)
Run Code Online (Sandbox Code Playgroud)
我有src和tests分开的目录。我遵循了在 Internet 上找到的配置,但它没有改变任何东西,我错过了什么?到目前为止唯一的方法是将我的tests文件夹包含在 中的include设置中tsconfig,这很糟糕,因为它内置在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"]
}
Run Code Online (Sandbox Code Playgroud)
jest.config.js
module.exports = {
roots: ['<rootDir>'],
preset: 'ts-jest',
testRegex: 'tests/src/.*\\.test.(js|jsx|ts|tsx)$',
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
transformIgnorePatterns: [],
snapshotSerializers: ['enzyme-to-json/serializer'],
moduleDirectories: ['node_modules', 'src', 'tests'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleNameMapper: {
'\\.(css|scss|jpg|png|svg)$': 'mocks/empty.ts',
},
setupFilesAfterEnv: ['<rootDir>/tests/jest.setup.ts'],
collectCoverage: true,
collectCoverageFrom: ['src/**/*.{js{,x},ts{,x}}', '!src/index.tsx', '!src/custom.d.ts'],
}
Run Code Online (Sandbox Code Playgroud)
只需包含您喜欢的jest内容即可:typeAcquisitiontsconfig.json
// tsconfig.json
{
"compilerOptions": { /* ... */ },
"typeAcquisition": { "include": ["jest"] },
// ... your other options go here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2205 次 |
| 最近记录: |