简体   繁体   中英

Cypress in TypeScript: Mocha afterEach: Stop Runner if currentTest failed

Goal is if stop Cypress runner of any test failed, and these Mocha tests are written in TypeScript. The following Mocha afterEach() has two issues...

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

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

Here are the issues:

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

How can I work this through in TypeScript and ignore it with // @ts-ignore ?

Thank you, appreciate the assistance.

Yes you can use // @ts-ignore. Also you will need to use regular function () {} syntax as opposed to the lambda “fat arrow” syntax () => {}

Refer Cypress document : 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 () => {}.

Code will look something like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM