Updated arrayContaining to require actual values to be arrays

If the actual value of a test was a string, this was matching against arrays
that contained the strings. This was due to the use of the contains matcher,
which against string looks for substrings, when it was intended to look for
array elements.
This commit is contained in:
David Diederich
2019-08-30 01:09:53 -04:00
parent 008b80adc5
commit 0bd636b5d2
2 changed files with 13 additions and 0 deletions

View File

@@ -31,6 +31,12 @@ describe("ArrayContaining", function() {
expect(containing.asymmetricMatch(["bar"])).toBe(false);
});
it("does not match when the actual is not an array", function() {
var containing = new jasmineUnderTest.ArrayContaining(["foo"]);
expect(containing.asymmetricMatch("foo")).toBe(false);
});
it("jasmineToStrings itself", function() {
var containing = new jasmineUnderTest.ArrayContaining([]);

View File

@@ -8,6 +8,13 @@ getJasmineRequireObj().ArrayContaining = function(j$) {
throw new Error('You must provide an array to arrayContaining, not ' + j$.pp(this.sample) + '.');
}
// If the actual parameter is not an array, we can fail immediately, since it couldn't
// possibly be an "array containing" anything. However, we also want an empty sample
// array to match anything, so we need to double-check we aren't in that case
if (!j$.isArray_(other) && this.sample.length > 0) {
return false;
}
for (var i = 0; i < this.sample.length; i++) {
var item = this.sample[i];
if (!j$.matchersUtil.contains(other, item, customTesters)) {