sinon fails when restoring stubbed properties in window object in IE11

Created on 22 Aug 2018  ·  4Comments  ·  Source: sinonjs/sinon

Describe the bug
it's possible to stub, but not to restore some properties in window object in IE11 causing sinon to throw a type error: TypeError: Cannot redefine non-configurable property '...'

To Reproduce
https://jsbin.com/qixexucasa/edit?js,console,output - compare behaviour in IE11 and any other browser.

"TypeError: Cannot redefine non-configurable property 'parent'
   at restore (https://unpkg.com/[email protected]/pkg/sinon.js:3253:13)
   at Global code (https://null.jsbin.com/runner:18:1)"

Expected behavior
In IE11 certain properties of window object are non-configurable, but it's still possible to stub them and this is quite useful in the tests. The expectable behaviour would be one of those:

  1. sinon should throw an error on the stub step (not on the restore() step) that it's not possible to stub this property;
  2. when stubbing the property sinon should not copy the original configurable attribute, is there any reason for this? In case of false value sinon just creates problems for itself, so it's not possible to restore it. So this line should be always true:
    https://github.com/sinonjs/sinon/blob/638e104d1e6ed9908c1b09bd33d2cba1aa8c217a/lib/sinon/default-behaviors.js#L251

Context:

  • Library version: all versions are affected, tested in 4.4.6 & 6.1.5.

Please confirm whether it's a bug or an expected behaviour.

Bug Help wanted hacktoberfest pinned

Most helpful comment

I having same problem and I found cause of this on sinon/default-behaviors.js line 258.

value: function value(fake, newVal) {
    var rootStub = fake.stub || fake;

    Object.defineProperty(rootStub.rootObj, rootStub.propName, {
        value: newVal,
        enumerable: true,
        configurable: isPropertyConfigurable(rootStub.rootObj, rootStub.propName)
    });

    return fake;
}

I don't know why it should follow original object's configurable option. I think it have to be always true because After it stubed, it can be restored or rewritten (typically sinon.restore()) regardless of original object's option.

Temporarily I fixed this problem by changing that code always return true.

All 4 comments

Thank you for your detailed description of the issue.

Would going with the second strategy actually make this work in IE11? If so, that would be my preference.

Would you be up for contributing a pull request to make the necessary changes?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I having same problem and I found cause of this on sinon/default-behaviors.js line 258.

value: function value(fake, newVal) {
    var rootStub = fake.stub || fake;

    Object.defineProperty(rootStub.rootObj, rootStub.propName, {
        value: newVal,
        enumerable: true,
        configurable: isPropertyConfigurable(rootStub.rootObj, rootStub.propName)
    });

    return fake;
}

I don't know why it should follow original object's configurable option. I think it have to be always true because After it stubed, it can be restored or rewritten (typically sinon.restore()) regardless of original object's option.

Temporarily I fixed this problem by changing that code always return true.

Was this page helpful?
0 / 5 - 0 ratings