[英]Retrieve the current test's name within a Mocha test using Typescript
这个问题与此非常相似,但是有一个非常重要的区别:使用了打字稿。
我正在尝试从mocha测试中获取当前的测试标题,但是由于使用了打字稿,因此此代码不起作用:
import 'mocha';
describe("top", () => {
console.log(this.title);
console.log(this.fullTitle());
it("test", () => {
console.log(this.test.title);
console.log(this.test.fullTitle());
});
});
打字稿掩盖this
和访问本地JavaScript的this
是不可能的了。
有人遇到过这种情况吗? 有什么解决方法吗?
您的问题不是使用TypeScript,而是使用箭头功能。
箭头函数会自动this
绑定到定义函数的this
。
因为它们都使用箭头功能,你this
是this
在全球层面,要么是发现了global
的严格模式之外,或undefined
严格模式。 (由于您使用的是ES模块,因此根据规格,您将自动进入严格模式)
进口“摩卡咖啡”;
describe("top", function() {
console.log(this.title);
console.log(this.fullTitle());
it("test", function() {
console.log(this.test.title);
console.log(this.test.fullTitle());
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.