cypress: Unable to add extra information to mocha test object

Current behavior:

I’m attempting to use mochawesome’s addContext method to add additional information to my tests. Debugging within the test, addContext is adding additional properties to the test object (this or runnable.ctx, but that information appears to be stripped out by the time it reaches the reporter. I raised this at mochawesome - adamgruber/mochawesome#242 - but it appears to be Cypress-specific.

Desired behavior:

When additional properties are added to the mocha test object, they should be passed to the reporter too.

Steps to reproduce:

Create a passing test and call addContext:

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

describe('example context missing', () => {
    it('should have context', function () {
        expect(1+1).to.eq(2);
        addContext(this, 'some context');
    });
});

Alternatively, call addContext on a failed test run by adding this to support/index.js, (based on #1200, which reported this was working but then follow-up comments say it stopped) and write a failing test elsewhere:

const addContext = require('mochawesome/addContext')

Cypress.on('fail', function(err, runnable) {
	addContext(runnable.ctx, 'failed context');
	throw err;
});

Versions

Cypress 2.10 running in Electron mochawesome 3.02 Windows 10 1803

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 14
  • Comments: 21 (1 by maintainers)

Most upvoted comments

Here’s another workaround for screenshots that doesn’t require you to manually construct the screenshot file names:

cypress/support/index.js

import addContext from 'mochawesome/addContext';

Cypress.on('test:after:run', (test, runnable) => {
  if ('failed' === test.state) {
    const screenshot = window.screenshots.pop();
    const root       = Cypress.config().reporterOptions.reportDir;

    screenshot && addContext({ test }, screenshot.replace(root, ''));
  }
});

cypress/support/hooks.js

afterEach('Maybe get screenshot path', function() {
  cy.task('screenshots', {log: false}).then(screenshots => {
    window.screenshots = screenshots;
  });
});

cypress/plugins/index.js

const screenshots = [];

module.exports = (on, config) => {
  on('after:screenshot', details => screenshots.push(details.path));

  on('task', {
    screenshots() {
     return screenshots;
    }
  });
};

Why is this issue closed? It seems like it hasn’t been fixed.

Hello,

Thanks to @LVCarnevalli 's post I’m now able to add extra information to the Mocha Context through Cypress Events as in his example, but I’m still not able to update the Mocha Context directly from a 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
    });
});

Did anyone manage to make it work ?

Yes. You can use it by creating a custom command this way:

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

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

And then, inside a test you can call it this way:

describe('example context missing', () => {
    it('should have context', function () {
        expect(1+1).to.eq(2);
        cy.addToReport('some element...');
    });
});

In your example it does not work because of the wrong context passed to addContext. You can check by viewing your tests console logs. It expects the param named as ‘test’. So addContext({ test: this }, element); should be enough.

Notice that if you use arrow functions they don’t bind the this context, so you’ll need to move to an ES5 function.

Actually, onfail does not return the context of the current test. Alternative, use:

Cypress.on('test:after:run', (test, runnable) => {
    if(test.state == 'failed') {
        addContext({ test }, `./screenshots/${runnable.parent.title} -- ${test.title} (failed).png`)
    }
});

Also remember to configure the screenshot (https://docs.cypress.io/guides/references/configuration.html#Screenshots) in cypress and run a rename linux command in the file (mv screenshots/*.feature/* screenshots/).

Observation: This code did not work for me, because it always came undefined.

Cypress.on('fail', function(err, runnable) {
    addContext(runnable.ctx, 'https://www.google.com')
    throw err
})

Looks like we just need to forward the context property.

We pluck off certain properties off of the runnables from the driver to send them to the background node process, so this just needs to be one of the additional properties we send.

@lots0logs i think i have it working now and did the following:

// Add visual testing to cypress with percy
const percyHealthCheck = require('@percy/cypress/task');

// Add context to mochawesome (screenshots on failure)
const screenshots = [];

module.exports = (on, config) => {
	on('after:screenshot', details => screenshots.push(details.path));
	
	on('task', {
		'screenshots' () {
			return screenshots;
		}
	});
	
	on('task', percyHealthCheck);
};

Thanks for all the help 👍

Hello,

Thanks to @LVCarnevalli 's post I’m now able to add extra information to the Mocha Context through Cypress Events as in his example, but I’m still not able to update the Mocha Context directly from a 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
    });
});

Did anyone manage to make it work ?