angular - 无法使用打字稿和角度为 Jasmine 添加自定义匹配器
问题描述
我正在尝试向 jasmine 添加多个新的自定义匹配器并使用它,目标是创建一个注册 2 个匹配器的文件,并且所有匹配器必须可用于每个测试文件,只需一次注册。
所以我有一个名为custom-matchers.ts
/// <reference path="custom-matchers.d.ts"/>
(() => {
beforeEach(() => {
jasmine.addMatchers({
toHaveText: () => {
return {
compare: (actual, expected) => {
return {
pass: actual.getText().then(pass => pass === expected)
};
},
};
},
toBePresent: () => {
return {
compare: (actual, expected) => {
return {
pass: actual.isPresent().then(pass => pass === expected),
};
},
};
}
});
});
})();
一个名为的类型定义文件custom-matchers.d.ts
(我尝试使用与引用文件不同的名称,但它是相同的):
declare namespace jasmine {
interface Matchers<T> {
toHaveText(text: any): boolean;
toBePresent(text: any): boolean;
}
}
该文件在另一个名为 e2e-helper 的文件中注册,该文件在每个测试文件中导入e2e-helper.ts
:
/// <reference path="custom-matchers.d.ts"/>
import './custom-matchers';
例如,当我尝试在测试文件中使用它时:
expect(object as any).toBePresent();
我收到一个错误:
“匹配器”类型上不存在属性“toBePresent”
解决方案
我找到了一个解决方案,我只是在文件下方的帮助文件中导入我的定义文件custom-matchers.ts
并将其重命名为具有不同的名称导入,所以我有:
import './custom-matchers';
import './custom-matchers-types'; // definition file
推荐阅读
- reactjs - 如何使用 antd 选项卡动态显示/隐藏按钮
- ios - 如何在鼠标拖动时将绘制的形状绑定为正方形
- python - 如何将数据框转换为字典
- c# - 在 SQL 数据库中进行手动更改时,Lucene.Net 索引更新
- angular - 在 Angular 上将文档作为经过身份验证的用户存储在 Firestore 中
- java - java中如何验证字符串只包含字母、数字、下划线和破折号
- android - 任务“:app:packageStageDebug”执行失败
- c - 需要帮助理解 xv6 中的 Makefile
- javascript - 如何在没有 php artisan serve 的情况下使用 laravel livewire?
- excel - 是否有 Excel Power Query 公式可以做“存在”或类似的事情?