Skip to content

Commit 81c23fb

Browse files
authored
Merge pull request #1523 from fatso83/1521-stub-array-filter-fails
Cache references to Array.prototype.filter
2 parents 3df68a7 + 052a2d0 commit 81c23fb

File tree

6 files changed

+42
-11
lines changed

6 files changed

+42
-11
lines changed

.github/PULL_REQUEST_TEMPLATE.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313
#### Solution - optional
1414
> When contributing code (and not just fixing typos, documentation and configuration), please describe why/how your solution works. This helps reviewers spot any mistakes in the implementation.
1515
>
16-
> Example:
16+
> Example:
1717
> "This solution works by adding a `paintBlue()` method"
1818
> Then your reviewer might spot a mistake in the implementation, if `paintBlue()` uses the colour red.
1919
2020
#### How to verify - mandatory
21-
1. Check out this branch (see github instructions below)
21+
1. Check out this branch
2222
2. `npm install`
2323
3. <your-steps-here>
24+
25+
#### Checklist for author
26+
27+
- [ ] `npm run lint` passes
28+
- [ ] References to standard library functions are [cached](https://github.com/sinonjs/sinon/pull/1523).

lib/sinon/call.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var functionName = require("./util/core/function-name");
66
var sinonFormat = require("./util/core/format");
77
var valueToString = require("./util/core/value-to-string");
88
var slice = Array.prototype.slice;
9+
var filter = Array.prototype.filter;
910

1011
function throwYieldError(proxy, text, args) {
1112
var msg = functionName(proxy) + text;
@@ -131,7 +132,7 @@ var callProto = {
131132

132133
yieldOn: function (thisValue) {
133134
var args = slice.call(this.args);
134-
var yieldFn = args.filter(function (arg) {
135+
var yieldFn = filter.call(args, function (arg) {
135136
return typeof arg === "function";
136137
})[0];
137138

@@ -148,7 +149,7 @@ var callProto = {
148149

149150
yieldToOn: function (prop, thisValue) {
150151
var args = slice.call(this.args);
151-
var yieldArg = args.filter(function (arg) {
152+
var yieldArg = filter.call(args, function (arg) {
152153
return arg && typeof arg[prop] === "function";
153154
})[0];
154155
var yieldFn = yieldArg && yieldArg[prop];

lib/sinon/collection.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ var sinonMock = require("./mock");
66
var collectOwnMethods = require("./collect-own-methods");
77
var valueToString = require("./util/core/value-to-string");
88

9-
var push = [].push;
9+
var push = Array.prototype.push;
10+
var filter = Array.prototype.filter;
1011

1112
function getFakes(fakeCollection) {
1213
if (!fakeCollection.fakes) {
@@ -18,7 +19,7 @@ function getFakes(fakeCollection) {
1819

1920
function each(fakeCollection, method) {
2021
var fakes = getFakes(fakeCollection);
21-
var matchingFakes = fakes.filter(function (fake) {
22+
var matchingFakes = filter.call(fakes, function (fake) {
2223
return typeof fake[method] === "function";
2324
});
2425

lib/sinon/mock.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var deepEqual = require("./util/core/deep-equal").use(match);
88
var wrapMethod = require("./util/core/wrap-method");
99

1010
var push = Array.prototype.push;
11+
var filter = Array.prototype.filter;
1112

1213
function mock(object) {
1314
if (!object || typeof object === "string") {
@@ -118,13 +119,13 @@ extend(mock, {
118119
var currentArgs = args || [];
119120
var available;
120121

121-
var expectationsWithMatchingArgs = expectations.filter(function (expectation) {
122+
var expectationsWithMatchingArgs = filter.call(expectations, function (expectation) {
122123
var expectedArgs = expectation.expectedArguments || [];
123124

124125
return arrayEquals(expectedArgs, currentArgs, expectation.expectsExactArgCount);
125126
});
126127

127-
var expectationsToApply = expectationsWithMatchingArgs.filter(function (expectation) {
128+
var expectationsToApply = filter.call(expectationsWithMatchingArgs, function (expectation) {
128129
return !expectation.met() && expectation.allowsCall(thisValue, args);
129130
});
130131

lib/sinon/spy.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ var wrapMethod = require("./util/core/wrap-method");
1212
var sinonFormat = require("./util/core/format");
1313
var valueToString = require("./util/core/value-to-string");
1414

15+
/* cache references to library methods so that they also can be stubbed without problems */
1516
var push = Array.prototype.push;
1617
var slice = Array.prototype.slice;
17-
var callId = 0;
18+
var filter = Array.prototype.filter;
1819
var ErrorConstructor = Error.prototype.constructor;
1920

21+
var callId = 0;
22+
2023
function spy(object, property, types) {
2124
var descriptor, methodDesc;
2225

@@ -340,7 +343,7 @@ var spyApi = {
340343
},
341344

342345
matchingFakes: function (args, strict) {
343-
return (this.fakes || []).filter(function (fake) {
346+
return filter.call(this.fakes || [], function (fake) {
344347
return fake.matches(args, strict);
345348
});
346349
},

test/issues/issues-test.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ describe("issues", function () {
321321
});
322322
});
323323

324-
describe("#1512", function () {
324+
describe("#1512 - sandbox.stub(obj,protoMethod)", function () {
325325
var sandbox;
326326

327327
beforeEach(function () {
@@ -341,4 +341,24 @@ describe("issues", function () {
341341
assert(stub.called);
342342
});
343343
});
344+
345+
describe("#1521 - stubbing Array.prototype.filter", function () {
346+
var orgFilter;
347+
348+
before(function () {
349+
orgFilter = Array.prototype.filter;
350+
});
351+
352+
afterEach(function () {
353+
/* eslint-disable no-extend-native */
354+
Array.prototype.filter = orgFilter;
355+
});
356+
357+
it("should be possible stub filter", function () {
358+
var stub = sinon.stub(Array.prototype, "filter");
359+
[1, 2, 3].filter(function () { return false; });
360+
assert(stub.calledOnce);
361+
});
362+
363+
});
344364
});

0 commit comments

Comments
 (0)