Skip to content

Cannot stub Array.prototype.filterΒ #1521

Closed

Description

@pomek

What did you expect to happen?
The tests should pass.

What actually happens

RangeError: Maximum call stack size exceeded
    at defineProperty (native)
    at Array.proxy (node_modules/sinon/lib/sinon/spy.js:69:79)
    at Function.matchingFakes (node_modules/sinon/lib/sinon/spy.js:343:35)
    at Function.invoke (node_modules/sinon/lib/sinon/spy.js:166:30)
    at Array.proxy (node_modules/sinon/lib/sinon/spy.js:69:54)
    at Function.matchingFakes (node_modules/sinon/lib/sinon/spy.js:343:35)
    at Function.invoke (node_modules/sinon/lib/sinon/spy.js:166:30)
    /* ... */   
    at Array.proxy (node_modules/sinon/lib/sinon/spy.js:69:54)
    at Function.matchingFakes (node_modules/sinon/lib/sinon/spy.js:343:35)
    at Function.invoke (node_modules/sinon/lib/sinon/spy.js:166:30)
    at Array.proxy (node_modules/sinon/lib/sinon/spy.js:69:54)
    at Context.it (packages/ckeditor5-dev-env/tests/asda.js:25:19)

How to reproduce

Just run the tests. It worked in Sinon 1.x and does not work in 2.x and 3.x.

Activity

  1. mroderick commented on Aug 9, 2017

    @mroderick
    Member

    Thank you for the detailed error report πŸ†

    Using your test case I have been able to determine that this defect was introduced in e72fee4, which was part of #1313 which became v2.3.6.

    Until this is fixed, I guess you can use v2.3.5.

  2. fatso83 commented on Aug 9, 2017

    @fatso83
    Contributor

    Looking at this line, it makes perfect sense that we get an infinite recursion when stubbing Array.prototype.filter, as we use that method internally. I suspect that would be the case for many other ES5 methods as well. The fix here would be as simple as rewriting it to a loop, or creating a filter utility method, but it leaves the question of what to do with all other methods we are using internally. Does this basically mean we have to refrain from using any of the standard library methods, as they may all be targets for stubbing? If so, this points to a bigger issue.

  3. mantoni commented on Aug 9, 2017

    @mantoni
    Member

    We could alternatively add something like var filter = Array.prototype.filter; at the top of the file and use it like filter.call(array, function () {}).

  4. fatso83 commented on Aug 9, 2017

    @fatso83
    Contributor

    @mantoni : yeah, sure we could (and many other ways), but I was just saying that we should probably make this the general rule of the codebase, as any method of the standard library could be a target for stubbing. I could start by adding it to the pull request template:

    All library methods ([].map, [].filter, toString, etc) needs to use cached references.

  5. mantoni commented on Aug 9, 2017

    @mantoni
    Member

    @fatso83 Yes, I agree. I suggested this solution as it's already used heavily with Array.prototype.slice.

  6. added 2 commits that reference this issue on Aug 10, 2017
    d8a2fc0
    531f872
  7. pomek commented on Aug 10, 2017

    @pomek
    Author

    Thanks, guys!

  8. mroderick commented on Aug 10, 2017

    @mroderick
    Member

    This has become part of v3.2.0

  9. added a commit that references this issue on Oct 1, 2019
    18093ab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions