Cypress版本12.14.0无法找到带有TypeScript的命令文件。

huangapple go评论74阅读模式
英文:

Cypress version 12.14.0 just cannot find command files with Typescript

问题

我正在尝试在我的Cypress测试中使用TypeScript包含自定义命令。似乎很难让它工作。我已经将文件移动到各种文件夹中,但这只是一次令人沮丧的体验。不应该这么难。这是我的项目结构:

在我的support文件夹中,我有以下文件:

cypress
  support
    e2e.ts
    index.d.ts // 声明Cypress命名空间并定义命令的定义
    accounts-cmd.ts // 包含Cypress.Command.add(.....)的文件
  fixtures
  tests // 我的测试在这里
    accounts.cy.ts // 使用自定义命令findByAccountsId(id: string)
// tsconfig.json
types: ["cypress", "./support"]
// cypress.config.ts
supportFile: "./support/e2e.ts"

在index.d.ts中,我声明了一个叫做findAccountsById(id: string)的命令,它返回一个Chainable,通过axios调用定义如下:

findByAccountId(id: string): Chainable<Response>;

在我的accounts-cmd.ts中,我引用了index.d.ts:

/// <reference path="./index.d.ts" />
英文:

I am attempting to include custom commands in my Cypress tests using typescript. It just seems so difficult to get it to work. I have moved the files in various folders but it is simply a frustrating experience. It shouldn't be this hard. Here is the structure of my project: I am using version 12.14.0 of Cypress and Typescript

In my support folder, I have the following files

         cypress
              support
                e2e.ts
                index.d.ts.     // declare namespace Cypress with definitions of commands 
                accounts-cmd.ts  // has Cypress.Command.add(.....)
              fixtures
              tests // my tests are in here
                accounts.cy.ts  // uses custom command findByAccountsId(id: string)
       //tsconfig.json
       types: [&quot;cypress&quot;, &quot;./support&quot;]

      //cypress.config.ts
       supportFile: &quot;./support/e2e.ts&quot;,


I have my a declaration in the index.d.ts called findAccountsById(id: string) which returns a Chainable<Response> from and axios call. It is defined like this


    findByAccountId(id: string): Chainable&lt;Response&gt;;


In my accounts-cmd.ts I reference the index.d.ts

    /// &lt;reference path=&quot;./index.d.ts&quot; /&gt;

答案1

得分: 4

我认为你想要将引用放在tsconfig.jsonincludes: []属性中。

请参考cypress-real-world-app中的示例:

{
  ...
  "include": ["./**/*.ts"],  // 包括 /cypress/support/index.d.ts
  ...
}

我理解"types"键用于节点模块,而不是你自己的类型。

英文:

I think you want to put the reference in includes: [] property of tsconfig.json.

See cypress-real-world-app

{
  ...
  &quot;include&quot;: [&quot;./**/*.ts&quot;],  // catches /cypress/support/index.d.ts
  ...
}

My understanding is that "types" key is for node modules, not your own types.

huangapple
  • 本文由 发表于 2023年6月25日 17:04:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76549673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定