javascript - 如何在全球范围内向茉莉花添加自定义匹配器?
问题描述
我需要替换jasmine.addMatchers
1.3 版中的功能。当前的 API 允许将匹配器添加到describe
块中,但我希望能够在任何地方使用我的匹配器,而无需一次又一次地添加它们。
有没有一种将自己的匹配器添加到 jasmine 3.1.0的全局方法?
解决方案
https://github.com/JamieMason/add-matchers可用于编写适用于所有版本的 Jasmine 以及 Jest 的匹配器。
var addMatchers = require('add-matchers');
addMatchers({
// matcher with 0 arguments
toBeEvenNumber: function(received) {
// received : 4
return received % 2 === 0;
},
// matcher with 1 argument
toBeOfType: function(type, received) {
// type : 'Object'
// received : {}
return Object.prototype.toString.call(received) === '[object ' + type + ']';
},
// matcher with many arguments
toContainItems: function(arg1, arg2, arg3, received) {
// arg1 : 2
// arg2 : 15
// arg3 : 100
// received : [100, 14, 15, 2]
return (
received.indexOf(arg1) !== -1 &&
received.indexOf(arg2) !== -1 &&
received.indexOf(arg3) !== -1
);
}
});
推荐阅读
- angular - 如何解决类型'Observable
' 不可分配给类型 'Observable '。类型“未知”不能分配给类型“字符串”? - python - 什么是 python 上下文中的`build`?
- fluentd - 从 fluentd 发送到 aws kinesis firehose 时数据丢失
- java - 如何使用 Java 将“多个二维数组”存储在单个文件中?
- pyspark - 使用 pyspark 到 Salesforce 的 Amazon S3
- zig - zig 结构、指针、字段访问
- kubernetes - 无法将 Docker CLI 连接到 minikube 内的远程 Docker 恶魔
- reactjs - 具有 Material UI React 的数据驱动选项卡
- java - 如何使用 JDBC/Java 访问和显示 MySQL 关系数据库中的特定记录?
- serialization - Solana Rust 程序 HashMap