Cypress: Still unable to add extra information to Mocha Test object

Created on 6 Mar 2019  Â·  9Comments  Â·  Source: cypress-io/cypress

Current behavior:

This is a follow-up of issue #1910.

We're using mochawesome in combination with Cypress in order to generate a HTML Report.
Mochawesome provides a feature to add information to the test context and have that information in the generated HTML Report. This is done through the addContext() function.

Thanks to @LVCarnevalli and his comment on #1910 it is possible to use the addContext() function from some Cypress Event Handler (test:after:run event from the comment) successfully.

But using the addContext() function directly from a test does not work. If I execute the following test:

const addContext = require('mochawesome/addContext');

describe('example context missing', () => {
    it('should have context', function () {
        expect(1+1).to.eq(2);
        addContext(this, 'some context'); // <== This is still not working
    });
});

I never get this _some context_ text in my generated report (HTML or JSON).

Desired behavior:

Calling addContext() function from a test should push new information to the test context.

Steps to reproduce: (app code and test code)

  • Follow the basic setup of Cypress until you have written your first test.
  • Integrate mochawesome to the basic Cypress setup.
  • Copy the test from above.
  • Execute and check the generated report.

Versions

cypress: 3.1.5
mocha: 5.2.0
mochawesome: 3.1.1
mochawesome-report-generator: 3.1.5

needs investigating reporters 📄

Most helpful comment

This has solved the issue for us:

it('Should shine the test report!!!', () => {
  cy.get('li').should('have.length.greaterThan', 0);
  addTestContext('String','giphy');
  addTestContext('Link','https://giphy.com');
  addTestContext('Image','https://media.giphy.com/media/tIIdsiWAaBNYY/giphy.gif');
  addTestContext('Image','https://media.giphy.com/media/tIIdsiWAaBNYY/giphy.gif');
});

function addTestContext(title, value) {
  cy.once('test:after:run', test => addContext({ test }, { title, value }));
}

https://stackoverflow.com/questions/53343181/detailed-reporting-cypress-mochawesome/53356856

All 9 comments

Exactly the same issue here, and it still exists with cypress 3.2.0.

Other versions:
mocha: 5.2.0
mochawesome: 3.1.1
mochawesome-merge: 1.0.7
mochawesome-report-generator: 3.1.5

I have the same issue. Any solutions?

Not yet. But as soon as I find the solution I’ll let you known.

On May 31, 2019, at 13:33, jhepam notifications@github.com wrote:

I have the same issue. Any solutions?

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

I posted a solution for adding screenshots to the mochawesome report. Perhaps it can be tweaked to suit your needs? You can check it out here: https://github.com/cypress-io/cypress/issues/1910#issuecomment-514067968

This has solved the issue for us:

it('Should shine the test report!!!', () => {
  cy.get('li').should('have.length.greaterThan', 0);
  addTestContext('String','giphy');
  addTestContext('Link','https://giphy.com');
  addTestContext('Image','https://media.giphy.com/media/tIIdsiWAaBNYY/giphy.gif');
  addTestContext('Image','https://media.giphy.com/media/tIIdsiWAaBNYY/giphy.gif');
});

function addTestContext(title, value) {
  cy.once('test:after:run', test => addContext({ test }, { title, value }));
}

https://stackoverflow.com/questions/53343181/detailed-reporting-cypress-mochawesome/53356856

Hi, more informations view question: https://stackoverflow.com/a/57928604/5495629

Still seeing this on cypress 4.5.0. If I debug into mochawesome code, it looks like the context field is being stripped somehow, it is lost at the point it reaches mochawesome. Anyone know if there's an official fix from cypress, or an alternative current "best" way to retain that field? Thanks in advance 🙂

Still seeing this on cypress 4.5.0. If I debug into mochawesome code, it looks like the context field is being stripped somehow, it is lost at the point it reaches mochawesome. Anyone know if there's an official fix from cypress, or an alternative current "best" way to retain that field? Thanks in advance 🙂

The best way until a fix comes (if one does) is to use the above: https://github.com/cypress-io/cypress/issues/3654#issuecomment-521887307

I've created a custom command:

import addContext from "mochawesome/addContext"

Cypress.Commands.add("addContext", (context) => {
  cy.once("test:after:run", (test) => addContext({ test }, context))
})

Then in your tests you can do:

cy.addContext("some additional context")

I've been looking to fix it in Cypress-proper but I haven't found a clear solution yet.

This post has been so helpful. I spent two days trying to figure out a solution without any success. Thanks to the contributors.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

weskor picture weskor  Â·  3Comments

brian-mann picture brian-mann  Â·  3Comments

EirikBirkeland picture EirikBirkeland  Â·  3Comments

brian-mann picture brian-mann  Â·  3Comments

jennifer-shehane picture jennifer-shehane  Â·  3Comments