【问题标题】:Cypress causes: Property 'each' does not exist on type 'TestFunction'赛普拉斯原因:“TestFunction”类型上不存在属性“每个”
【发布时间】:2022-03-31 14:44:46
【问题描述】:

我们在项目中引入了 cypress 9.3.1 进行 e2e 测试。现在我们面临的问题是我们现有的 jest 测试无法在 CI 中编译。

所有参数化测试都会出现以下错误:

Property 'each' does not exist on type 'TestFunction'.
       it.each<TestCase>([

问题:如何解决?


我们尝试过但没有奏效的方法:

  • 在每个测试中添加import { it } from '@jest/globals'。我们可以通过在每个测试中添加import { expect } from '@jest/globals' 来解决类似的问题(“断言”类型上不存在属性“toBeTruthy”)。见:https://stackoverflow.com/a/65153905

  • 通过将 "exclude": ["cypress/global.d.ts"] 添加到 tsconfig.spec.json 来为 cypress 全局变量添加项目范围的排除项

【问题讨论】:

    标签: typescript jestjs dependencies cypress


    【解决方案1】:

    刚刚也遇到了这个问题,结果发现我们的tsconfig.json 中包含了来自 mocha 的冲突类型定义。确保您没有加载 Mocha 的类型定义。

    编辑:我们最终需要为我们的 cypress 测试创建一个单独的 tsconfig.json,如下所述:https://docs.cypress.io/guides/tooling/typescript-support#Configure-tsconfig-json

    【讨论】:

      【解决方案2】:

      我们正在使用 here 描述的 tsconfig.json(请参阅 @JRJurman 的回答),但这在我们的项目中不起作用。

      现在我们正在使用由 cypress 贡献者之一开发的 cypress-each 插件。

      npm i -D cypress-each
      

      用法:

      import 'cypress-each'
      
      // create a separate test for each selector
      const selectors = ['header', 'footer', '.new-todo']
      it.each(selectors)('element %s is visible', (selector) => {
        cy.visit('/')
        cy.get(selector).should('be.visible')
      })
      

      【讨论】:

        猜你喜欢
        • 2021-11-05
        • 1970-01-01
        • 2020-08-03
        • 2018-06-29
        • 1970-01-01
        • 1970-01-01
        • 2021-10-05
        • 2020-04-14
        • 1970-01-01
        相关资源
        最近更新 更多