问题描述
目标是如果停止任何测试的 Cypress runner 失败,这些 Mocha 测试是用 TypeScript 编写的。下面的摩卡 $_SESSION['logged-in']
有两个问题...
afterEach()
问题如下:
-
/// <reference types="Cypress" /> afterEach(() => { if (this.currentTest.state === 'Failed' && this.currentTest._currentRetry === this.currentTest._retries) { Cypress.runner.stop(); } });
>>> 引用this.currentTest.*
>>>this
-
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();
}
});