The Problem:
I have an array of promises which is resolved to an array of strings. Now the test should pass if at least one of the strings matches a regular expression.
Currently, I solve it using simple string concatenation:
protractor.promise.all([text1, text2, text3]).then(function (values) {
expect(values[0] + values[1] + values[2]).toMatch(/expression/);
});
Obviously, this does not scale well and is not particularly readable.
The Question:
Is is possible to solve it using a custom jasmine matcher, or jasmine.any()
or custom asymmetric equality tester?
You could simply use map
to get a list of boolean
and then assert the result with toContain(true)
:
var all = protractor.promise.all;
var map = protractor.promise.map;
expect(map(all([text1, text2, text3]), RegExp.prototype.test, /expression/)).toContain(true);
You could also use a custom matcher:
expect(all([text1, text2, text3])).toContainPattern(/expression/);
And the custom matcher declared in beforeEach
:
beforeEach(function() {
jasmine.addMatchers({
toContainPattern: function() {
return {
compare: function(actual, regex) {
return {
pass: actual.some(RegExp.prototype.test, regex),
message: "Expected [" + actual + "] to have one or more match with " + regex
};
}
};
}
});
});