Typescript 在Cypress中,对于一个状态(给定)和一个结果(然后)的相同黄瓜定义,哪一个是最佳实践?

Typescript 在Cypress中,对于一个状态(给定)和一个结果(然后)的相同黄瓜定义,哪一个是最佳实践?,typescript,cucumber,cypress,cucumberjs,cypress-cucumber-preprocessor,Typescript,Cucumber,Cypress,Cucumberjs,Cypress Cucumber Preprocessor,对于某些步骤定义使用Cucumber.js和Cypress的最佳实践存在一些疑问 基本上,测试需要一个状态和一个可以相同执行的结果: 这是一个国家的例子: Given( 'the {int} item of the {string} component is visible', (index, ComponentIndex, mapper: ComponentMapper) => { cy.get('[data-test^="cmp-"]').eq(cm


对于某些步骤定义使用
Cucumber.js
Cypress
的最佳实践存在一些疑问

基本上,测试需要一个状态和一个可以相同执行的结果:

这是一个国家的例子:

Given(
  'the {int} item of the {string} component is visible',
  (index, ComponentIndex, mapper: ComponentMapper) => {
    cy.get('[data-test^="cmp-"]').eq(cmpMap[mapper]).as(mapper)
    cy.get(`@${mapper}`)
      .find('.component__item')
      .eq(index)
      .should('be.visible')
  }
)
这是一个结果的例子:

Then(
  'the {int} item of the {string} component is visible',
  (index, ComponentIndex, mapper: ComponentMapper) => {
    cy.get('[data-test^="cmp-"]').eq(cmpMap[mapper]).as(mapper)
    cy.get(`@${mapper}`)
      .find('.component__item')
      .eq(index)
      .should('be.visible')
  }
)
在这种情况下,我只看到代码重复,因为
给定的
然后执行相同的代码,执行相同的检查。我想了解所有可能的解决方案,以最有效的方式实现这一点

双重实施 保留描述中定义的双步骤
赞成:更清晰
缺点:双重编码\

单一实现 在
给定的
然后的
中保留一个实现,因为cucumber实际上不检查特征文件中的步骤与定义文件中与步骤相关的函数的匹配
赞成:代码的单一实现
缺点:步骤定义不太清晰,更混乱

外部功能 创建一个函数,例如将其放入Cypress或UTIL的某些命令中,并在两种定义中使用它
赞成:单一实现
缺点:设计过度?在Cucumber.js中从未见过这种方法


检查一下,你建议哪一个?谢谢

还有一个选项,基本上是“单一实现”,但更清楚一点。我假设您使用的是
cypress Cumber预处理器
,除了
给定
/
/
然后
,您还可以导入
定义步骤
,它做的事情完全相同,但从代码的角度来看,更清楚的是,以这种方式定义的步骤是通用的

import { defineStep } from "cypress-cucumber-preprocessor/steps";

defineStep('the {int} item of the {string} component is visible', 
    (index, ComponentIndex, mapper: ComponentMapper) => {
    ...
});