Create better error for missing decorated getters
See original GitHub issueIn the case of using the decorate syntax (in an environment where decorators aren’t enabled), there is a really confusing error message.
For example, in the documentation on how to not use decorators, you have this example:
class Timer {
start = Date.now();
current = Date.now();
get elapsedTime() {
return this.current - this.start + "milliseconds";
}
tick() {
this.current = Date.now();
}
}
decorate(Timer, {
start: observable,
current: observable,
elapsedTime: computed,
tick: action
});
If you delete the get elapsedTime(){
function, you get this error:
Uncaught TypeError: Cannot read property 'get' of undefined
at Object.propertyCreator (mobx.module.js:596)
at initializeInstance$$1 (mobx.module.js:373)
at MobxStore.set [as activeFile] (mobx.module.js:358)
at new MobxStore (MobxStore.js:33)
The problem is that the elapsedTime: computed
property doesn’t get deleted with the function, which is easy to do when they aren’t co-located (without decorators). This keeps happening to me and I’ve memorized the error, but the first couple times where hard to track down. It seems like there is no intentional error checking - it just errors down the stack at instantiation of the class.
I think it should error (give the dev a chance to clean up cruft) but give them more information why it’s throwing an error.
Thank you
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:29 (10 by maintainers)
If you get the
property is not declared configurable
error when usingJest spyOn
on mobx@computed
properties, you can add this:Thanks @wlindner 😉
PS: it’s documented here https://mobx.js.org/refguide/api.html
@zetoke, I don’t like settings in at all in generally, but it might actually be a nice idea in this case, to have it a ‘test only’ setting. Something like
mobx.configure({ enableStubbing: true })
?