championswimmer / vuex-module-decorators

TypeScript/ES7 Decorators to create Vuex modules declaratively
https://championswimmer.in/vuex-module-decorators/
MIT License
1.8k stars 170 forks source link

Stubbing getters #207

Open alecgibson opened 4 years ago

alecgibson commented 4 years ago

Given a module:

export default class MyModule extends VuexModule {
  get propertyA() {
    // Do something
  }

  get propertyB() {
    // Do something with propertyA
  }
}

If I'd like to stub propertyA for testing propertyB, I'd normally write:

sinon.stub(MyModule, 'propertyA').get(() => {...});

However, in this case the getter is non-configurable, so I just get:

TypeError: Cannot redefine property: propertyA

Is there any way to make these getters configurable (just in tests, obviously)?

alecgibson commented 4 years ago

Okay, so current hacky workaround:

sinon.stub(MyModule.store, 'getters').value({
  ...MyModule.store.getters,
  'myModule/propertyA': () => 'foo',
});

It would be nice if we could just set the getters as configurable, though, maybe as an option in the @Module decorator? eg

@Module({name: 'myModule', namespaced: true, stateFactory: true, mockable: true})
export default class MyModule extends VuexModule {}
mariosh346 commented 4 years ago

did you find a better way?

alecgibson commented 4 years ago

@mariosh346 nope, this is still my best approach 😞