As you discovered, accessing this
inside the callback for it
does not work. Here's one way to do it:
describe('this message text', function () {
var suite_name = this.title;
var test_name;
beforeEach(function () {
test_name = this.currentTest.title;
});
it('and this message text', function () {
console.log(suite_name, test_name);
});
it('and this other message text', function () {
console.log(suite_name, test_name);
});
});
The workaround in the code above is that the beforeEach
hook grabs the test name before the test is run and saves it in test_name
.
If you wonder what the value of this
is inside a test callback, it is the value of the ctx
field on the suite to which the test belongs. For instance, the console.log
statement in this:
describe('suite', function () {
this.ctx.foo = 1;
it('test', function () {
console.log(this);
});
});
would output:
{
"foo": 1
}