【问题标题】:Cypress in TypeScript: Mocha afterEach: Stop Runner if currentTest failedTypeScript 中的 Cypress:Mocha afterEach:如果 currentTest 失败则停止运行
【发布时间】:2020-12-31 00:34:59
【问题描述】:

目标是如果停止任何测试的 Cypress 运行器失败,并且这些 Mocha 测试是用 TypeScript 编写的。下面的摩卡afterEach()有两个问题...

/// <reference types="Cypress" />

  afterEach(() => {
    if (this.currentTest.state === 'failed' && 
      this.currentTest._currentRetry === this.currentTest._retries) {
      Cypress.runner.stop();
    }
  });

以下是问题:

  1. this.currentTest.* >>> 参考this >>> TS2532: Object is possibly 'undefined'
  2. Cypress.runner.stop() >>> TS2339: Property 'runner' does not exist on type 'Cypress'

如何在 TypeScript 中解决这个问题并使用 // @ts-ignore 忽略它?

谢谢,感谢您的帮助。

【问题讨论】:

    标签: typescript mocha.js cypress


    【解决方案1】:

    是的,您可以使用 // @ts-ignore 。您还需要使用常规函数 () {} 语法,而不是 lambda “胖箭头”语法 () => {}

    参考赛普拉斯文档:https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Avoiding-the-use-of-this

    Accessing aliases as properties with this.* will not work 
    if you use arrow functions for your tests or hooks.  This is why all of our
    examples use the regular function () {} syntax as opposed to the
    lambda “fat arrow” syntax () => {}.
    

    代码看起来像这样

    afterEach(function() {
        if (this.currentTest.state === 'failed' && 
          //@ts-ignore
          this.currentTest._currentRetry === this.currentTest._retries) {
          //@ts-ignore
          Cypress.runner.stop();
        }
      });
    

    【讨论】:

    • 感谢您的回复,我会检查一下。我尽量不使用//@ts-ignore。对我来说,这就像忽略存在可能有问题的问题。
    • 如果您查看 cypress 项目 'utilities.spec.js' 中的示例。 //@ts-忽略 TS2351 作为注释。您可以将其与支持性评论一起添加,并将其添加为审核过程的一部分。任何人都不应在没有评论和审阅者批准的情况下使用 //@ts-ignore。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多