我的(Typescript)应用程序识别我已安装的包的类型的 Cypress 测试遇到问题。这是我的顶级目录结构:
cypress
node_modules
src
我的 cypress/tsconfig.json
文件如下所示:{
"compilerOptions": {
"strict": true,
"baseUrl": "../node_modules",
"paths": {
"~/*": ["../src/*"]
},
"jsx": "react",
"target": "es6",
"lib": ["es6", "dom", "es2017.string"],
"types": ["cypress"]
},
"include": ["**/*.ts"]
}
在我的一个规范文件中,我有以下导入:import * as faker from 'faker';
import { DeepPartial } from 'utility-types';
faker
的类型在 DefinedTypes ( @types/faker
) 中定义,而 utility-types
的类型包含为 *.d.ts
该包中的文件。 faker
导入没问题,但是utility-types
import 给出了一个找不到模块“实用程序类型”或其相应的类型声明。错误。我已经尝试明确包括
*.d.ts
从 node_modules 目录到 tsconfig.json
的文件compilerOptions.types
下的文件, compilerOptions.typeRoots
, 和 include
属性,但无济于事。我还创建了如下所示的“假”(?)类型,以便编译:
declare module 'utility-types' {
export type DeepPartial<T> = {};
}
这允许应用程序编译并在运行时解析包,因此问题似乎在于查找类型,而不是模块本身。为什么 Cypress 没有找到这些包的类型?
最佳答案
您可以尝试设置moduleResolution
至node
https://www.typescriptlang.org/docs/handbook/module-resolution.html#module-resolution-strategies
我已尝试导入 utility-types
使用此配置:导入有效,并且测试运行正确
{
"compilerOptions": {
"strict": true,
"baseUrl": "../node_modules",
"paths": {
"~/*": ["../src/*"]
},
"jsx": "react",
"target": "es6",
"lib": ["es6", "dom", "es2017.string"],
"types": ["cypress"],
"moduleResolution": "node"
},
"include": ["**/*.ts"]
}
关于javascript - Cypress 测试无法识别 node_modules 包类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63742910/