赛普拉斯测试无法识别 node_modules 包类型

im1*_*ike 6 javascript typescript tsconfig cypress

我的(Typescript)应用程序在 Cypress 测试中遇到问题,无法识别已安装的包的类型。这是我的顶级目录结构:

cypress
node_modules
src
Run Code Online (Sandbox Code Playgroud)

我的cypress/tsconfig.json文件看起来像这样:

{
  "compilerOptions": {
    "strict": true,
    "baseUrl": "../node_modules",
    "paths": {
      "~/*": ["../src/*"]
    },
    "jsx": "react",
    "target": "es6",
    "lib": ["es6", "dom", "es2017.string"],
    "types": ["cypress"]
  },
  "include": ["**/*.ts"]
}
Run Code Online (Sandbox Code Playgroud)

在我的规范文件之一中,我有以下导入:

import * as faker from 'faker';
import { DeepPartial } from 'utility-types';
Run Code Online (Sandbox Code Playgroud)

for 的类型faker在绝对@types/faker类型( )中定义,而 for 的类型utility-types作为*.d.ts文件包含在该包中。该faker进口没有问题,但utility-types进口是给找不到模块“公用事业型”或其相应类型的声明。错误。

我试着明确地包括*.d.ts从node_modules目录中的文件tsconfig.json的文件下compilerOptions.typescompilerOptions.typeRootsinclude性能,但无济于事。

我还创建了如下所示的“假”(?)类型,以便编译:

declare module 'utility-types' {
  export type DeepPartial<T> = {};
}
Run Code Online (Sandbox Code Playgroud)

这允许应用程序编译,并且在运行时,包得到解决,因此问题似乎在于查找类型,而不是模块本身。

为什么赛普拉斯找不到这些包的类型?

dna*_*dna 3

您可以尝试设置moduleResolutionnode

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"]
  }
Run Code Online (Sandbox Code Playgroud)