0

I am trying to get the result of the test (each scenario ) in After hook [Referring https://stackoverflow.com/questions/66153949/get-test-status-after-each-test-in-cypress]

Cypress.mocha.getRunner().suite.ctx.currentTest.state

I am getting error "Property 'mocha' does not exist on type Cypress & CyEventEmitter"

I am using Cypress,Typescript with cucumber ,but since mocha is default with cypress , cypress.mocha should work in after hook

Any idea in how to fix this issue?

tsconfig.json file

{
  "compilerOptions": {
                                
    "target": "es2016",                                
    "lib": ["es6"],                               
    "module": "commonjs",                               
    "types": ["cypress","cypress-xpath","node"],                                 
     "resolveJsonModule": true,                       
    "esModuleInterop": true,                             
    "strict": true,                                     
    "forceConsistentCasingInFileNames": true,  
    "skipLibCheck": true 
  }
}

Do I need change anything in tsconfig.json file ?

Update : I could solve the error(TypeScript Property 'mocha' does not exist on type Cypress & CyEventEmitter) by using [Referring link https://github.com/cypress-io/cypress/issues/2972#issuecomment-671779458] (Cypress as any).mocha.getRunner().suite...

But I am now facing Cannot read properties of undefined (reading 'state') Any help on this ?

Update: Cannot read properties of undefined (reading 'state') This resolved after replacing after hook with afterEach,Complete Resolution Provided in the Comment section

Note I am using cypress 12,node 18,cypress-cucumber-preproccesor 4.3

Sagarika
  • 31
  • 7

3 Answers3

4

That code appears to be out of date.

I'm using cy.state('test'), here's an example

describe('the suite', () => {
  it('test one', () => {
    expect(true).to.eq(true)

    const { title, state, parent: suite } = cy.state('test')
    console.log(title, state, suite.title)  // "test one" undefined "the suite"
  })

  after(() => {
    const { title, state, parent: suite } = cy.state('test')
    console.log(title, state, suite.title)    // "test one" "passed" "the suite"
  })
})

Actually, your original seems to work too, you just need to add mocha types to the project

npm install --save @types/mocha

tsconfig.json

{
  ...
  "types": ["cypress","cypress-xpath","node","mocha"], 
}
Lola Ichingbola
  • 3,035
  • 3
  • 16
  • Thank you for your response , cy.state('test') will not work in my case Since I am using Cucumber BDD (not using describe and it block ) , I tried the other way installing @types/mocha and added it in tsconfig as you mentioned , still I am getting same error "mocha" does not exist on type cypress – Sagarika May 18 '23 at 09:17
  • Any other way to fetch the test result ? – Sagarika May 18 '23 at 09:18
  • No, @Sagarika you are wrong, `cy.state('test')` works fine in cucumber just like any Cypress command. – Elizabeth Gilpin May 18 '23 at 09:54
  • @ElizabethGilpin To me it says property state does not exist on type 'cy and cyEventEmitter' Is there any configuration required to use this – Sagarika May 18 '23 at 10:26
4

You can add the missing type definitions in the same way that you add a type definition for a custom command (see the Typescript Support page)

Create a type definition file cypress.d.ts in the project root.

export {}

declare global {
  namespace Cypress {
    interface cy {
      state: any;              // for cy.state()
    }
    interface Cypress {
      mocha: any;              // for Cypress.mocha
    }
  }
}

Add a reference to it in tsconfig.json

{
  "compilerOptions": {
    ...
  },
  "include": ["**/*.ts", "cypress.d.ts"]
}
Fody
  • 23,754
  • 3
  • 20
  • 37
0

From Cypress 6.0, the cy.state() command has been removed from the public API and it is no longer available to access the internal state of Cypress at runtime. Cypress intentionally restricts access to its internal state to prevent test code from depending on or modifying internal implementation details. For Cypress 12 to get the title and status of test case I found the following code

afterEach(function() {
 const name = Cypress.currentTest.title
 cy.log(name)
 const sceanrioStatus=(Cypress as any).mocha.getRunner().suite.ctx.currentTest.state
 cy.log(sceanrioStatus)
});

In the above code Cypress.currentTest.title will fetch the name of the test case (in my case sceanrio name) and state will fetch the pass/fail status

Sagarika
  • 31
  • 7
  • It's a fair enough comment in general, but there's nothing I can find to back up your claim. Cypress have public examples using `cy.state()` for instance [here](https://docs.cypress.io/api/cypress-api/custom-queries#focused) and [here](https://docs.cypress.io/guides/guides/test-retries#Can-I-access-the-current-attempt-counter-from-the-test). – Paolo May 24 '23 at 06:05
  • 1
    The only deprecation noted in the Changelog is in [v10.5.0](https://docs.cypress.io/guides/references/changelog#10-5-0) - ***`cy.state('subject')` is deprecated and reading from it will log a warning to the console. Prefer `cy.currentSubject()` instead.*** – Paolo May 24 '23 at 06:07