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)
Here’s another workaround for screenshots that doesn’t require you to manually construct the screenshot file names:
cypress/support/index.jscypress/support/hooks.jscypress/plugins/index.jsWhy is this issue closed? It seems like it hasn’t been fixed.
Yes. You can use it by creating a custom command this way:
And then, inside a test you can call it this way:
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
thiscontext, so you’ll need to move to an ES5 function.Actually, onfail does not return the context of the current test. Alternative, use:
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.
Looks like we just need to forward the
contextproperty.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:
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.
Did anyone manage to make it work ?