TypeScript 中的 Cypress:Mocha afterEach:如果 currentTest 失败,则停止 Runner

问题描述

目标是如果停止任何测试的 Cypress runner 失败,这些 Mocha 测试是用 TypeScript 编写的。下面的摩卡 $_SESSION['logged-in'] 有两个问题...

afterEach()

问题如下:

  1. /// <reference types="Cypress" /> afterEach(() => { if (this.currentTest.state === 'Failed' && this.currentTest._currentRetry === this.currentTest._retries) { Cypress.runner.stop(); } }); >>> 引用 this.currentTest.* >>> this
  2. TS2532: Object is possibly 'undefined' >>> Cypress.runner.stop()

如何在 TypeScript 中解决这个问题并使用 TS2339: Property 'runner' does not exist on type 'Cypress' 忽略它?

谢谢,感谢您的帮助。

解决方法

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

参考 Cypress 文档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();
    }
  });

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...