javascriptnode.jscypressmochawesome

Cypress - addContext() is keeping previous failure count and adding it to each 'it' scenario in mochawesome report


In my mochawesome-report addContext() is keeping previous count and adding it to each 'it' scenario, in case of a test case failure, I'm adding 'someValue' as context to the test case. So if 2nd test case fails then value is getting printed twice.

Following is the snapshot:

enter image description here

Following is my afterEach() method :

afterEach(function () {
    if (this.currentTest.state === 'failed') {    
      var test = this.currentTest

      Cypress.on('test:after:run', (test) => {

        addContext({ test }, {
          title: 'Failing Screenshot: ' + '>> screenshots/' + Cypress.spec.name + '/' + test_name + ' -- ' + test.title + ' (failed)' + '.png <<',
          value: 'screenshots/' + Cypress.spec.name + '/' + test_name + ' -- ' + test.title + ' (failed)' + '.png'
          //value: 'data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAABkCAYAAAAVORraAAACH0lEQVR'
        })
      });
    } 
  })

Solution

  • Got what I was looking from https://docs.cypress.io/api/events/catalog-of-events.html#Cypress-Events

    enter image description here

    Though I'll have to remove Cypress.on('test:after:run', afterEach()

    So I'll have to specify Cypress.on('test:after:run', in each spec file

    const spec_name = this.title
    
      Cypress.on('test:after:run', (test) => {
    
        if (test.state === 'failed') {
          addContext({ test }, {
            title: 'Failing Screenshot: ' + '>> screenshots/' + Cypress.spec.name + '/' + spec_name + ' -- ' + test.title + ' (failed)' + '.png <<',
            value: 'screenshots/' + Cypress.spec.name + '/' + spec_name + ' -- ' + test.title + ' (failed)' + '.png'
          })
        }
      });
    

    which is kind of put off, it would be better to put this whole code in support/command.js