protractor - 对象可能是“未定义” - Mocha
问题描述
我正在使用量角器。以下解决方案有效,但我收到此警告:
this.currentTest.state
- 错误 TS2532:对象可能是“未定义”(属性)Mocha.Context.currentTest?:Mocha.Test | 不明确的
我该如何解决这个警告?
测试文件:
const helper = new HelperClass();
afterEach(async ()=> {
const state = this.currentTest.state;
await helper.getSource(state);
});
类文件
import { browser, } from 'protractor';
export class HelperClass {
public getSource(state:any) {
if (state === 'failed') {
browser.driver.getPageSource().then(function (res) {
console.log(res);
});
}
}
}
解决方案
我认为发生错误是因为访问this.currentTest.state
发生在另一个函数内部:传递给afterEach
--flow 分析的箭头函数没有跨越函数边界。尝试简单地将那条线拉到函数之外:
const helper = new HelperClass();
afterEach(async ()=> {
const state = this.!currentTest.state;
await helper.getSource(state);
});
这有什么改变吗?
推荐阅读
- c++ - 单击按钮 QT 时将参数传递给函数
- autohotkey - 遇到无法处理自动热键的事件时如何使脚本超时
- c++ - C++中具有不同符号的数量的算术和比较的最佳方法
- html - 铬缩放导致元素失真
- javascript - 如何允许用户使用 Jquery/AJAX 在您的网站中创建/生成新网页?
- reactjs - 使用 prevState 和 useState 钩子更新对象内部数组中的对象
- python - 如何覆盖 Django 上的 ImageField 小部件
- substrate - 将资产托盘集成到节点模板派生链中时导致签名错误的类型问题
- hibernate - Hibernate - 如何在数据库端持久化计算列
- mysql - 使用 MYSQL 中的引用表替换子字符串