I’d need a replacement for the jasmine.addMatchers
function gone in version 1.3. The current API allows to add matchers to a describe
block, but I’d prefer to be able to use my matchers everywhere without adding them again and again.
Is there a global way to add own matchers to jasmine 3.1.0?
Advertisement
Answer
https://github.com/JamieMason/add-matchers can be used to write matchers which work in all versions of Jasmine, as well as Jest.
JavaScript
x
28
28
1
var addMatchers = require('add-matchers');
2
3
addMatchers({
4
// matcher with 0 arguments
5
toBeEvenNumber: function(received) {
6
// received : 4
7
return received % 2 === 0;
8
},
9
// matcher with 1 argument
10
toBeOfType: function(type, received) {
11
// type : 'Object'
12
// received : {}
13
return Object.prototype.toString.call(received) === '[object ' + type + ']';
14
},
15
// matcher with many arguments
16
toContainItems: function(arg1, arg2, arg3, received) {
17
// arg1 : 2
18
// arg2 : 15
19
// arg3 : 100
20
// received : [100, 14, 15, 2]
21
return (
22
received.indexOf(arg1) !== -1 &&
23
received.indexOf(arg2) !== -1 &&
24
received.indexOf(arg3) !== -1
25
);
26
}
27
});
28