This question is very similar to this one, but has one very significant difference: typescript is used.
I'm trying to get current test title from within the mocha test, but since typescript is used this code does not work:
import 'mocha';
describe("top", () => {
console.log(this.title);
console.log(this.fullTitle());
it("test", () => {
console.log(this.test.title);
console.log(this.test.fullTitle());
});
});
Typescripts obscures this and access to native JavaScript's this is not possible anymore.
Has anyone faced this situation before? Is there any workaround for it?
Your problem isn't that you use TypeScript, it's that you use arrow functions.
Arrow functions automatically bind
thisto thethiswhere the function is defined.Since they all use arrow functions, your
thisis thethisfound at the global level, which is eitherglobaloutside of strict mode, orundefinedin strict mode. (Since you're using ES modules, per-spec you are automatically in strict mode)import 'mocha';