【问题标题】:Cypress tests not recognizing node_modules package types赛普拉斯测试无法识别 node_modules 包类型
【发布时间】:2020-12-23 19:54:09
【问题描述】:

我的 (Typescript) 应用程序的赛普拉斯测试识别我已安装的包的类型时遇到问题。这是我的顶级目录结构:

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 的类型在 DefinedlyTypes (@types/faker) 中定义,而 utility-types 的类型作为 *.d.ts 文件包含在该包中。 faker 导入没有问题,但 utility-types 导入给出了 Cannot find module 'utility-types' 或其对应的类型声明。错误。

我已尝试将 node_modules 目录中的 *.d.ts 文件显式包含到 compilerOptions.typescompilerOptions.typeRootsinclude 属性下的 tsconfig.json 文件中,但无济于事。

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

declare module 'utility-types' {
  export type DeepPartial<T> = {};
}

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

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

【问题讨论】:

  • 尝试将以下内容添加到您的.tsconfig"include": [ "node_modules/cypress/types/index.d.ts", "node_modules/cypress/types/blob-util.d.ts", "node_modules/cypress/types/minimatch.d.ts", "node_modules/cypress/types/bluebird.d.ts", "cypress/**/*.ts" ]
  • 作为参考,我查看了一个带有工作 TS 环境的示例 repo:github.com/cypress-io/cypress-example-recipes/blob/master/…
  • @RaghavKukreti 我应该提到除了typestypeRoots 之外,我还尝试了include tsconfig 属性。它不起作用。

标签: javascript typescript cypress tsconfig


【解决方案1】:

您可以尝试将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"]
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-05
    • 2021-10-11
    • 2023-02-10
    • 2020-12-23
    • 2023-02-14
    • 2021-10-20
    • 2020-05-16
    • 1970-01-01
    相关资源
    最近更新 更多