--- 更新---- 我可以使用命令行运行测试,但无法在 VSCode 中使用 Jest Runner 扩展运行测试。
------原始问题-----------------
我认为我已经损坏了我的 npm 模块,因为我尝试在我的项目中安装一些更新版本的 jest 和 ts-jest 相关模块(希望修复一些与测试相关的问题),但它导致了很多其他问题,所以我只是恢复了
package.json
和 package.lock.json
。
现在在我的测试文件中,它不再识别
jest
。 Typescript simple 显示了一堆错误,抱怨它找不到 jest
,找不到 describe
(在笑话中定义)。
我尝试删除node_modules文件夹并运行
npm install
,但这没有帮助。我清除了 npm 缓存;这没有帮助。
不知道我还能尝试什么。
这是我的 tsconfig.json
{
"compilerOptions": {
/* Basic Options */
"target": "es6",
"module": "commonjs",
"outDir": "./dist",
"allowJs": true,
"importHelpers": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"sourceMap": true,
/* Strict Type-Checking Options */
"strict": true,
"noImplicitAny": false,
"esModuleInterop": true,
"experimentalDecorators": true,
"types": [
"node",
"jest"
],
"baseUrl":".",
"paths": {
"@/*": [
"src/*"
]
},
},
"include": [
"src/**/*.ts",
"*.ts",
"src/**/*.json"
]
}
抱歉,但不确定除了这种特殊情况下的屏幕截图之外我还能分享什么。
以防万一这对将来的某人有帮助。通过将
"**/*.spec.ts"
添加到 include
中的
tsconfig.json
即可解决该问题
不知道为什么需要这个,因为它以前工作得很好;我想这一定是因为我做了一些部分升级。
当我必须测试我的项目时,我总是做以下事情。我会尝试一步步解释。
1- 删除 package.json 中与 Karma/Jasmine 相关的所有内容:
npm remove <karma karma-chrome-launcher...>
在 <> 内键入与这些库相关的所有内容。
2- 安装 Jest:
npm install --save-dev jest jest-preset-angular @types/jest
3- 在项目的根文件夹中创建setup-jest.ts,并将其放入其中:
import 'jest-preset-angular/setup-jest';
4- 将 jest 配置添加到 package.json:
"jest": {
"preset": "jest-preset-angular",
"setupFilesAfterEnv": [
"<rootDir>/setup-jest.ts"
],
"globalSetup": "jest-preset-angular/global-setup"
}
5- 在 tsconfig.json 和 tsconfig.spec.json 中配置 JEST,添加以下内容:
"types": [
"jest"
]
6- 配置命令来测试您的应用程序:
"test": "jest",
"test:watch": "jest --watchAll",
7- 删除 karma.config.json 和 test.ts。
如果您使用 Angular,这应该可以工作。我不使用其他框架/库,所以我真的不知道你是否是这样做的。
就我而言,我必须通过将
"types": [ "node", "jest" ]
添加到 compilerOptions
中的
tsconfig.json
对象来添加笑话类型