Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can no longer stub window methods using sinon #478

Closed
kevinkucharczyk opened this issue Jun 8, 2022 · 1 comment · Fixed by #535
Closed

Can no longer stub window methods using sinon #478

kevinkucharczyk opened this issue Jun 8, 2022 · 1 comment · Fixed by #535

Comments

@kevinkucharczyk
Copy link

Starting from Sinon version 11.1.0, it is no longer possible to stub properties and methods on window, as suggested in the readme:

let stub = sinon.stub(window, 'confirm');
stub.returns(true);

This will throw an error:
CleanShot 2022-06-08 at 14 42 46

TypeError: Cannot read properties of undefined (reading 'get')
    at getAccessor (webpack://__ember_auto_import__/./node_modules/sinon/lib/sinon/util/core/wrap-method.js?:1:1061)
    at extendObjectWithWrappedMethods (webpack://__ember_auto_import__/./node_modules/sinon/lib/sinon/util/core/wrap-method.js?:8:208)
    at wrapMethod (webpack://__ember_auto_import__/./node_modules/sinon/lib/sinon/util/core/wrap-method.js?:8:87)
    at Function.stub (webpack://__ember_auto_import__/./node_modules/sinon/lib/sinon/stub.js?:3:1569)
    at Sandbox.stub (webpack://__ember_auto_import__/./node_modules/sinon/lib/sinon/sandbox.js?:8:720)
    at Object.<anonymous> (http://localhost:13123/assets/tests.js:41707:22)

I tracked it down to this PR in Sinon itself sinonjs/sinon#2378, which shipped in version 11.1.0. More specifically, it seems that Sinon may have a bug when trying to mock/stub properties on Proxy objects.

This may be related to issues like #99

I understand that this may be for Sinon to fix, but it would be worth updating the readme to account for this. Maybe include a built-in option to easily mock and restore window methods/properties?

@simonihmig
Copy link
Owner

Thanks for reporting!

I wonder what happens when you directly set the property, instead of letting stub do that, like (untested code, just off the top of my head):

let fake = sinon.fake();
fake.returns(true);
window.confirm = fake;

simonihmig added a commit that referenced this issue Jun 13, 2023
This updates sinon dependencies. By doing that, it also reproduced #478, which needs to get fixed in a separate PR (skipping test for now)
simonihmig added a commit that referenced this issue Jun 13, 2023
The proxy did not support all handlers like `getOwnPropertyDescriptor` and `defineProperty`, which did not play nicely with the latest sinon versions. Fixes #478
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants