question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to add extra information to mocha test object

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

7reactions
lots0logscommented, Jul 27, 2019

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;
    }
  });
};
6reactions
albertmircommented, Oct 1, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to add information to successful tests in mocha
Show activity on this post. You could use the afterEach hook and add logging informations from an object of test result created during...
Read more >
Expect / Should - Chai Assertion Library
When the target is an object, .include asserts that the given object val 's properties are a subset ... See the .a doc...
Read more >
Testing Node.js with Mocha and Chai - LogRocket Blog
Commonly used with Chai, Mocha is an open source JavaScript testing framework that runs on Node.js and in the browser.
Read more >
Unit test reports - GitLab Docs
To copy the name of all failed tests, at the top of the Test summary panel, select Copy failed tests. The failed tests...
Read more >
Automated testing with Mocha - The Modern JavaScript Tutorial
We add more use cases to the spec, probably not yet supported by the implementations. Tests start to fail. Go to 3, update...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found