diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 197929cb..5a8e11b4 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -2810,7 +2810,9 @@ getJasmineRequireObj().asymmetricEqualityTesterArgCompatShim = function(j$) { for (i = 0; i < props.length; i++) { k = props[i]; - if (k !== 'length') { + // Skip length (dealt with above), and anything that collides with + // MatchesUtil e.g. an Array.prototype.contains method added by user code + if (k !== 'length' && !self[k]) { copy(self, Array.prototype, k); } } diff --git a/spec/core/asymmetricEqualityTesterArgCompatShimSpec.js b/spec/core/asymmetricEqualityTesterArgCompatShimSpec.js index c7c4d9b6..7a9a039c 100644 --- a/spec/core/asymmetricEqualityTesterArgCompatShimSpec.js +++ b/spec/core/asymmetricEqualityTesterArgCompatShimSpec.js @@ -36,7 +36,6 @@ describe('asymmetricEqualityTesterArgCompatShim', function() { it('provides properties of Array.prototype', function() { var keys = [ 'concat', - 'constructor', 'every', 'filter', 'forEach', @@ -55,8 +54,6 @@ describe('asymmetricEqualityTesterArgCompatShim', function() { 'some', 'sort', 'splice', - 'toLocaleString', - 'toString', 'unshift' ], optionalKeys = [ @@ -98,4 +95,49 @@ describe('asymmetricEqualityTesterArgCompatShim', function() { } } }); + + describe('When Array.prototype additions collide with MatchersUtil methods', function() { + function keys() { + return [ + 'contains', + 'buildFailureMessage', + 'asymmetricDiff_', + 'asymmetricMatch_', + 'equals', + 'eq_' + ]; + } + + beforeEach(function() { + keys().forEach(function(k) { + if (Array.prototype[k]) { + console.log(Array.prototype[k].toString()); + } + expect(Array.prototype[k]) + .withContext('Array.prototype already had ' + k) + .toBeUndefined(); + Array.prototype[k] = function() {}; + }); + }); + + afterEach(function() { + keys().forEach(function(k) { + delete Array.prototype[k]; + }); + }); + + it('uses the MatchersUtil methods', function() { + var matchersUtil = new jasmineUnderTest.MatchersUtil({}), + shim = jasmineUnderTest.asymmetricEqualityTesterArgCompatShim( + matchersUtil, + [] + ); + + keys().forEach(function(k) { + expect(shim[k]) + .withContext(k + ' was overwritten') + .toBe(jasmineUnderTest.MatchersUtil.prototype[k]); + }); + }); + }); }); diff --git a/src/core/asymmetricEqualityTesterArgCompatShim.js b/src/core/asymmetricEqualityTesterArgCompatShim.js index 162d9651..e487f3df 100644 --- a/src/core/asymmetricEqualityTesterArgCompatShim.js +++ b/src/core/asymmetricEqualityTesterArgCompatShim.js @@ -66,7 +66,9 @@ getJasmineRequireObj().asymmetricEqualityTesterArgCompatShim = function(j$) { for (i = 0; i < props.length; i++) { k = props[i]; - if (k !== 'length') { + // Skip length (dealt with above), and anything that collides with + // MatchesUtil e.g. an Array.prototype.contains method added by user code + if (k !== 'length' && !self[k]) { copy(self, Array.prototype, k); } }