Improve error message given when a property is only exposed as a getter
fatso83 opened this issue · comments
Is your feature request related to a problem? Please describe.
Confusion arises when module exports are not transpiled into an expected form . Typically you expect module.myExport = stub()
to make sense, but if myExport
is really just a getter you will not be able to assign to that prop. See #2530
Describe the solution you'd like
We should try to detect such cases and give some more meaningful error message, given the context. We could have some elaboration on our homepage and then link to that in the error message.
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
if trying to to wrap an undefined property; then
if property name has a getter; then
output hint that the property `propName` is undefined, but it is defined as a getter, so you might want to try `sandbox.stub(obj, propName).get(() => customStub)`
else
output a generic error about undefined property
endif
endif
Additional context
See #2530
@fatso83 how we can reproduce this issue? and alternatively, how can we find out that property is getter or setter if it's undefined at first place?
@ahmed1hsn You are correct in pointing this out! This description is both wrong and it actually made me aware of a bug inside of Sinon. I had a look at the error message that was originally mentioned in #2530 to see what was really going on and found that it does a typeof wrappedMethod
, and that will always be undefined
as nothing has set that variable yet.
Additionally, as you point out, if something has a getter, it is defined. I must have been a bit sleep deprived when writing this initially. Adding an actual reproduction case (as I could not do that in 2530) is indeed the first step 😄 And the this issue needs updating.
This issue does not make sense as it is, being based on an issue we could not reproduce and having some faulty assumptions.