Unable to AddContext in mochawesome reporter.
Closed this issue ยท 4 comments
Unable to use the addContext from mochawesome as the Context Object is changed while using yadda, can anyone help.
Hi @BIPLAVGHOSAL, are you able to provide some more details please? Some steps to reproduce would also be useful. Thanks.
Sure, @cressie176 Sorry for opening the issue without steps. ๐ข
So, I am using the test example which is given in the yadda docs,(Link) and instead of the spec reporter, I am trying to use an npm package mochawesome
for reporting which gives me an HTML report.
In that HTML report, we have the capability to add our desired output, for example, and order number from any website or any details which can be useful after the test result.
But when I am trying to use the feature it's complaining that
[mochawesome] Error adding context: Invalid test object.
Steps to reproduce:
Replace the
./steps/bottles-library.js
file with the below
var English = require('yadda').localisation.English;
const addContext = require('mochawesome/addContext');
var Wall = require('../../lib/wall'); // The library that you wish to test
module.exports = (function() {
return English.library()
.given("$NUM green bottles are standing on the wall", function(number, next) {
wall = new Wall(number);
addContext(this, 'simple string'); // THIS IS THE ADDITIONAL STEP I AM TRYING
next();
})
.when("$NUM green bottle accidentally falls", function(number, next) {
wall.fall(number);
next();
})
.then("there are $NUM green bottles standing on the wall", function(number, next) {
assert.equal(number, wall.bottles);
next();
});
})();
After that, this is the output in the console
NOTE: I have tried the addContext method directly with mocha-> describe -> it and it works fine.
Hope the steps are fine now. Thanks for the help!!
Hi @BIPLAVGHOSAL,
Thanks for the clarification. My first guess would be that addContext
requires this
to be the value from inside mocha's it
function, not the value of this
from one of Yadda's step functions. Let me explain...
Behind the scenes both Yadda and Mocha do something like the following
function foo() {
console.log(this);
}
const obj = {};
const fn = foo.bind(obj);
After binding foo
to obj
, the value this
becomes a reference to obj
when used within the scope of the function foo
.
In the case of Yadda, the obj
is an object Yadda uses to share state between steps. In the case of Mocha obj
an object that represents the test currently being executed. They are two different things, and not interchangeable.
addContext
expects a mocha test object, but because you are passing in the yadda shared state, then mocha awesome is reporting an error. To get this to work, you need to pass the mocha test object into the yadda step, then use this when calling addContext
You can do this as follows...
scenarios(feature.scenarios, function(scenario) {
steps(scenario.steps, function(step, done) {
yadda.run(step, { mocha: this }, done);
});
});
library.then(/I do something/, function() {
addContext(this.mocha, 'simple string');
});
Let me know if this works. If not I'll take a deeper look later on.
Hello @cressie176 .. Thanks a lot for the wonderful explanation. ๐
and Good news, it works by the above approach, just attached a screenshot below for reference and closing the Issue..
Thanks a lot. ๐