当将TypeScript与Jest结合使用时,我的规范会失败,错误消息如下:
test/unit/some.spec.ts:1:1 - error TS2582: 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`.
test/unit/some.spec.ts:2:3 - error TS2582: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.
test/unit/some.spec.ts:3:7 - error TS2304: Cannot find name 'expect'.
test/unit/some.spec.ts:7:1 - error TS2582: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.类型已经安装好了。
我用:
"@types/jest": "^23.3.12",
"jest": "^23.6.0",
"ts-jest": "^23.10.5",
"typescript": "^3.1.6"我使用jest --forceExit --coverage --verbose运行测试
发布于 2019-01-11 01:24:23
在玩弄了tsconfig.json一段时间之后,我终于意识到,对"types": [],进行评论将是可行的。
配置失败(之前)
// tsconfig.json
{
"compilerOptions": {
"types": []
}
}工作配置
// tsconfig.json
{
"compilerOptions": {
// "types": []
}
}发布于 2019-02-11 04:21:59
我正在使用Visual作为我的IDE和我的角度项目,我不得不在文件tsconfig.json中注释/删除类型,并在tsconfig.spec.json类型中添加"jest“。
文件tsconfig.json
{
"compilerOptions": {
// "types": []
}
}文件tsconfig.spec.json
{
"compilerOptions": {
"types": ["jest", "node"]
}
}发布于 2020-04-08 18:32:05
这有点棘手,因为两者都是:您的IDE (即Visual )和TypeScript都将tsconfig.json用于自己的目的。
解决初始问题的简单清单:
( TypeScript和Jest)
@types/jest和@types/node。tsconfig.json中链接了这些类型,以便:"types": ["jest", "node"]excluded属性中的tsconfig.json配置之外。转移溢出的副作用
如果您使用TypeScript或任何依赖于tsconfig.json的自定义模块从tsc传输到JavaScript,那么您可能会注意到在这种情况下您的测试也会被转移(您将在构建目录中看到它们的.js通信)。
但是,在大多数情况下,您可以选择:
tsconfig.prod.json。有许多设置,如inlineSource、sourceMaps、inlineSourceMaps,您可能也希望禁用这些设置,然后使用tsc --project tsconfig.prod.json构建npx tsc --inlineSourceMap false --declarationMap false --inlineSources false --sourceMap false.此时,您可以使用--excludeFiles或--excludeDirectories标志将您的测试排除在构建根据文件之外。另一种方法是使用包(如里姆拉夫 )和删除不必要的文件作为构建过程的一部分。它可能不像覆盖配置那么复杂,而且更容易作为构建步骤来维护。在这种情况下,您可以使用命令:yarn rimraf build/**/*.test.js。
https://stackoverflow.com/questions/54139158
复制相似问题